2016-04-13 9 views
-1

投稿からいくつかの入力値を取得して配列に格納したいと考えています。php sortをデータを配列に投稿する

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

どのように各iによってグループ化することができます:私は(フィールドが繰り返されている)は、次の取得形式としますprint_rを送信すると

<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[]" /> 

:ここでは、反復フィールドです私の入力要素であり、繰り返し申請?

だから、例えば私は、このような出力期待:

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

すべてのヘルプは、おかげでいただければ幸いです!

+1

'foreach'ループを使用します –

+0

あなたは例がありますか? – danyo

答えて

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

これは、(要素名に[] []に注意してください)トリックを行う必要があります。..

<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

これは動作しません。 –

+0

正解、回答を更新しました – LMS94

5

をあなたは、より簡単に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]" /> 

これは、このなります

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

グループごとに固定名を使用すると、グループを動的に追加/削除するのがずっと難しくなります。 'name =" items [1] [buyproduct] "'​​のようなものを使うとそれを解決できます。 – jeroen

+0

それから、 'item [0] [otemquantity]'という名前だけで、常にすべてを二重に配列することができます。 '$ _POST ['item']'は必要に応じて配列を格納します –

+0

私はちょうどそれを追加しました:-) – jeroen

0

ヨ:PHPのポスト変数で既存の$_POSTデータを取り出し、適切な構造に並べ替えることができます。したがって、$_POSTデータをループし、新しい配列に目的の出力を設定します。

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

forループまたはforeachループを使用できます。あなたはこのようなことをすることができます

<?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>"; 
?> 
関連する問題