从数组中提取相对键的值

从数组中提取相对键的值

问题描述:

我的数组组合从数组中提取相对键的值

FORM结果拉入此类型的$ _POST数组。

位置[0] =>数组,示出了箱的确切键 - [0] =>数组(1,4,5) -

你好! 通过形式我的数组提取物是:

Array 
    (
     [chk] => Array 
      (
       [0] => 1 
       [1] => 4 
       [2] => 5 
      ) 

     [ID] => Array 
      (
       [0] => 1 
       [1] => 2 
       [2] => 3 
       [3] => 4 
       [4] => 5 
      ) 

     [firstAttr] => Array 
      (
       [0] => Sun 
       [1] => Love 
       [2] => Fruit 
       [3] => Dog 
       [4] => Sky 
      ) 

     [secondAttr] => Array 
      (
       [0] => Big 
       [1] => intense 
       [2] => Delicious 
       [3] => Black 
       [4] => Blue 
      ) 

     [otherAttr] => Array 
      (
       [0] => White 
       [1] => Red 
       [2] => Orange 
       [3] => Old 
       [4] => Nice 
      )  
    ) 

我的结果请求是基团[CHK]。

我只需要取出属于[chk]组的值。

例如:

[chk] => Array 
    (
     [0] => 1 
     [1] => 4 
     [2] => 5 
    ) 

ID [0] => 1, firstAttr [0] => Sun, secondAttr [0] => Big, otherAttr [0] => White 
ID [3] => 4, firstAttr [3] => Dog, secondAttr [3] => Black,otherAttr [3] => Old 
ID [4] => 5, firstAttr [4] => Sky, secondAttr [4] => Blue, otherAttr [4] => Nice 

结果阵列。

我从萃取[INPUT TYPE = “复选框” 名称= “CHK []”]的值1,4,5

现在我已经通过参照这些键来提取值:

在'例如它们是:[chk] =>数组(1,4,5)。

RESULT [chk] group: 
    1 = Sun, Big, White 
    4 = Dog, Black, Old 
    5 = Sky, Blue, Nice 

$selectAll = $_POST; 
$chk = $_POST['chk']; 
$chkcount = count($chk); 

$result = array(); 
foreach ($chk as $index) { 
    $result[$index]['ID'] = $selectAll['ID'][$index-1]; 
    $result[$index]['firstAttr'] = $selectAll['firstAttr'][$index-1]; 
    $result[$index]['secondAttr'] = $selectAll['secondAttr'][$index-1]; 
    $result[$index]['otherAttr'] = $selectAll['otherAttr'][$index-1]; 
} 

print_r($result); 
+0

$ CHK = $ _POST [0] [ 'CHK'];试试这个 – Sona

+0

什么是预期的结果。清楚地说明 –

尝试下面的代码:

<?php 

$res =[ 
    'chk' => [ 1,4,5 ], 

    'ID' =>[1,2,3,4,5 ], 

    'firstAttr' => [ 

      'Sun', 
      'Love', 
      'Fruit', 
      'Dog', 
      'Sky' 
     ], 

    'secondAttr' => [ 

      'Big', 
      'intense', 
      'Delicious', 
      'Black', 
      'Blue' 
     ], 

    'otherAttr' => [ 
      'White', 
      'Red', 
      'Orange', 
      'Old', 
      'Nice' 
     ] 
]; 

$result = []; 
foreach($res['chk'] as $value){ 
    $key = $value -1; 
    $result[$value] = $value.' = '.$res['firstAttr'][$key].','.$res['secondAttr'][$key].','.$res['otherAttr'][$key]; 
} 

print_r($result);