2017-01-30 6 views
-1

mein Array bestehtextrahieren Wert für relativen Schlüssel von Array

Das FORM-Ergebnis zieht in $ _POST Array dieses Typs.

Position [0] => Array, zeigt mir die genaue Tasten - [0] => Array (1,4,5) -

Hallo! mein Array Extrakt nach Form ist:

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 
      )  
    ) 

mein Ergebnis Anforderung Gruppe [CHK].

Ich muss nur die Werte herausnehmen, die zur [chk] Gruppe gehören.

Beispiel:

[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 

Ergebnis-Array.

I aus extrahiert [input type = "checkbox" name = "chk []"] die Werte 1,4,5

jetzt habe ich die Werte extrahieren, indem Sie auf diese Tasten verweisen:

im Beispiel sind das: [chk] => Array (1,4,5). Versuchen

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']; versuchen Sie diese – Sona

+0

was erwartetes Ergebnis ist. Stellen Sie das klar fest –

Antwort

0

den folgenden Code:

<?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); 
Verwandte Themen