2014-01-10 48 views
0

我有一個來自表單POST的數組,我想將它們組合在一起。使用表單域創建PHP數組

我的第一次嘗試是在數組上運行一個foreach循環,但我沒有成功將邏輯分組。我有用按鍵編號創建動態數組的想法,但我無法獲取該數字。

初始陣列:

["affix-1-order"]=> "1"; 
["affix-1-type"]=> "Apple"; 
["affix-1-count"]=> "5"; 
["affix-3-order"]=> "2"; 
["affix-3-type"]=> "Orange"; 
["affix-3-count"]=> "10"; 
["affix-2-order"]=> "3"; 
["affix-2-type"]=> "Banana"; 
["affix-2-count"]=> "3"; 
["affix-4-order"]=> "4"; 
["affix-4-type"]=> "Mango"; 
["affix-4-count"]=> "15"; 

預期輸出:

["1"]=> [{ 
    ["type"]=> "Apple", 
    ["count"]=> "5", 
    ["order"]=> "1" 
}], 
["2"]=> [{ 
    ["type"]=> "Banana", 
    ["order"]=> "3", 
    ["count"]=> "3" 
}], 
["3"]=> [{ 
    ["type"]=> "Orange", 
    ["order"]=> "2", 
    ["count"]=> "10", 
}], 
["4"]=> [{ 
    ["type"]=> "Mango", 
    ["order"]=> "4", 
    ["count"]=> "15" 
}]; 
+2

讓你有在後所需的數組,你可以建立自己的形式進行任何操作上 – 2014-01-10 03:12:07

+0

後者我不明白?你如何建議我在表單輸入中分組鍵和3個值? – veksen

+0

'name = X [] ['type']''name = X [] ['order']''name = X [] ['count']',帖子X數組將會是你想要的 – 2014-01-10 03:35:48

回答

2

我寧願遵循達貢的建議和修改形式或發送所述數據Ajax請求。

通過在字段名稱中使用方括號,您可以讓PHP爲您完成工作。

<?php 
if(count($_POST)) 
{ 
    print_r ($_POST['data']); 
    die(); 
} 
?> 

<body onload='document.getElementById("post").submit();'> 
    <form method="POST" id="post"> 
     <input type='text' name='data[0][type]' value='Apple'> 
     <input type='text' name='data[0][count]' value='5'> 
     <input type='text' name='data[0][order]' value='1'> 
     <input type='text' name='data[1][type]' value='Banana'> 
     <input type='text' name='data[1][count]' value='3'> 
     <input type='text' name='data[1][order]' value='3'> 
     <input type='text' name='data[2][type]' value='Orange'> 
     <input type='text' name='data[2][count]' value='2'> 
     <input type='text' name='data[2][order]' value='10'> 
     <input type='text' name='data[3][type]' value='Mango'> 
     <input type='text' name='data[3][count]' value='4'> 
     <input type='text' name='data[3][order]' value='15'> 
    </form> 
</body> 

輸出

[0] => Array 
     [type] => Apple 
     [count] => 5 
     [order] => 1 
[1] => Array 
     [type] => Banana 
     [count] => 3 
     [order] => 3 
[2] => Array 
     [type] => Orange 
     [count] => 2 
     [order] => 10 
[3] => Array 
     [type] => Mango 
     [count] => 4 
     [order] => 15 
+0

沒有足夠的閱讀,看到這是來自'POST'形式。這將是更好的選擇。 – Sam

+0

@SamSullivan OTOH我的解決方案達不到正則表達式:) –

+0

我不知道這是可能的......謝謝你,你只是救了我幾個小時的工作:) – veksen

3
$array = array(
    "affix-1-order" => "1", 
    "affix-1-type" => "Apple", 
    "affix-1-count" => "5", 
    "affix-3-order" => "2", 
    "affix-3-type" => "Orange", 
    "affix-3-count" => "10", 
    "affix-2-order" => "3", 
    "affix-2-type" => "Banana", 
    "affix-2-count" => "3", 
    "affix-4-order" => "4", 
    "affix-4-type" => "Mango", 
    "affix-4-count" => "15", 
); 

$final = array(); 
foreach($array as $key => $value) { 
    preg_match('/affix\-([\d]+)\-(.*)/', $key, $matches); 
    // $matches[0] = 'affix-1-order'; 
    // $matches[1] = '1'; 
    // $matches[2] = 'order'; 

    $final[$matches[1]][$matches[2]] = $value; 
} 

print_r($final); 
// Array (
// [1] => Array ([order] => 1 [type] => Apple [count] => 5) 
// [3] => Array ([order] => 2 [type] => Orange [count] => 10) 
// [2] => Array ([order] => 3 [type] => Banana [count] => 3) 
// [4] => Array ([order] => 4 [type] => Mango [count] => 15) 
//) 
+0

我結束了修改表單,但這是一個很棒的解決方案,謝謝! :) – veksen

+0

沒問題@veksen,修改表單是一個更好的解決方案。但它總是很好學習一點正則表達式;) – Sam

+1

正則表達式是definitly我的下一個大躍進 – veksen