2016-04-13 24 views
-1

Ich möchte einige Eingabewerte von Post erhalten und sie in einem Array speichern. Hier sind meine Eingabeelemente, die wiederholbare Felder:php sortiere Postdaten in Arrays

<input type="text" class="form-control" id="exampleInputPassword1" name="itemquantity[]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="buyproduct[]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="freeproduct[]" /> 

Wenn ich das Formular abschicken und print_r ich die folgende (die Felder haben wiederholt worden):

Array ([itemquantity] => Array ( 
           [0] => 1 
           [1] => 4 
           ) 
     [buyproduct] => Array ( 
           [0] => 2 
           [1] => 5 
          ) 
     [freeproduct] => Array ( 
           [0] => 3 
           [1] => 6 
           ) 

Wie kann ich gruppieren sie von jedem wiederholt eingereicht?

zum Beispiel so würde ich eine Ausgabe wie folgt erwartet:

Array(
    Array [0](
    [itemquantity] => 1 
    [buyquantity] => 2 
    [freeproduct] => 3 
) 
    Array [1](
    [itemquantity] => 4 
    [buyquantity] => 5 
    [freeproduct] => 6 
) 
) 

Jede Hilfe würde geschätzt, danke!

+1

Verwenden Sie 'foreach'-Schleife –

+0

haben Sie ein Beispiel? – danyo

Antwort

1
$result = array(); 
foreach ($_POST['itemquantity'] as $k => $v) { 
    $result[] = array(
     'itemquantity' => $v, 
     'buyquantity' => $_POST['buyquantity'][$k], 
     'freeproduct' => $_POST['freeproduct'][$k], 
    ); 
} 
0

Dies sollte den Trick (beachten Sie die [] [] in den Elementnamen) ..

<input type="text" class="form-control" id="exampleInputPassword1" name="[][itemquantity]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="[][buyproduct]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="[][freeproduct]" /> 
+0

Nein, das wird nicht funktionieren. –

+0

Korrekt, ich habe meine Antwort aktualisiert – LMS94

5

Sie können diese leichter in PHP:

<input type="text" class="form-control" id="exampleInputPassword1" name="item1[itemquantity]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item1[buyproduct]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item1[freeproduct]" /> 

<input type="text" class="form-control" id="exampleInputPassword1" name="item2[itemquantity]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item2[buyproduct]" /> 
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item2[freeproduct]" /> 

Dies wird dies geworden in PHP post-Variable:

Array(
    Array [item1](
     [itemquantity] => 1 
     [buyquantity] => 2 
     [freeproduct] => 3 
    ) 
    Array [item2](
     [itemquantity] => 4 
     [buyquantity] => 5 
     [freeproduct] => 6 
    ) 
) 
+0

Die Verwendung fester Namen für verschiedene Gruppen macht es viel schwieriger, Gruppen dynamisch hinzuzufügen/zu entfernen. Etwas wie 'name =" items [1] [buyproduct] "' ​​würde das lösen. – jeroen

+0

dann können Sie immer alles mit nur einem Namen verdoppeln: 'item [0] [otemquantity]'. '$ _POST ['item']' wird das Array nach Bedarf enthalten –

+0

Ich habe gerade hinzugefügt, dass :-) – jeroen

0

Yo Sie können die vorhandenen $_POST Daten übernehmen und in eine geeignete Struktur umordnen. Durchlaufen Sie die Daten $_POST und füllen Sie ein neues Array mit der gewünschten Ausgabe.

$output = array(); $i = 0; 
foreach($_POST['itemquantity'] as $v) { 
    $output[] = array(
    'itemquantity' => $v, 
    'buyquantity' => $_POST['buyquantity'][$i], 
    'freeproduct' => $_POST['freeproduct'][$i] 
); 
    $i++; 
} 
0

können Sie für Foreach-Schleife verwenden. Sie können so etwas tun,

<?php 
$a1 = array(1,2,3); 
$a2 = array(4,5,6); 
$new_array[] = array(); 
for($i=0; $i<count($a1); $i++){ 
    $new_array[$i]['a1'] = $a1[$i]; 
    $new_array[$i]['a2'] = $a2[$i]; 
} 
echo "<pre>"; 
print_r($new_array); 
echo "</pre>"; 
?> 
Verwandte Themen