2017-01-30 2 views
-1

мой массив составлензначение извлечения для относительного ключа из массива

Результат FORM вытаскивает массив $ _POST этого типа.

положение [0] => Массив, показывает мне точные ключи - [0] => Array (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 

Результат массива.

Я извлек из [тип ввода = «флажок» имя = «CHK []»] значения 1,4,5

теперь приходится извлекать значения путем ссылки на эти клавиши:

Например, они: [chk] => Array (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

, что ожидается результат. Ясно, что –

ответ

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); 
Смежные вопросы