Demand
Now there is a set of two-dimensional arrays:
Array (8) {[0]=> Array (2) { ["name"]=> string (4) "Name" ["Value"]=> string (6) "Green Leaf"} [1]=> Array (2) { ["name"]=> string (5) "Phone" ["Value"]=> string (one) "13812341234"} [2]=> Array ( 2) { ["name"]=> string () "fileds_507[]" ["Value"]=> string (12) "I Am Green Leaf"} [3]=> Array (2) { ["Name"]=> string (fileds_508[] " [" Value "]=> string (6)" Hefei "} [4]=> Array (2) { ["Name"]=> string () "fileds_509[]" ["Value"]=> string (3) "Male"} [5]=> Array (2) { ["Name"]=> string () "fileds_510[]" ["Value"]=> string (6) "Soccer"} [6]=> Array (2) { ["Name"]=> string () "fileds_510[]" ["Value"]=> string (6) "Baseball"} [7]=> Array (2) { ["Name"]=> string (fileds_511[] " [" Value "]=> string (+)" 2016-12-15t11:15 "}}
You need to merge value of the array with the same value as the second-dimensional key named name, forming a new array.
For example, the above code in the name of fileds_510 two-dimensional array, it should be combined into a value for the soccer, baseball array.
Ideas
Referring to the array, and then in PHP we first think of the loop, then it is obviously more suitable for the use for loop processing, similar to the bubble sort, one by one, the best processing.
Code
The code is as follows:
$public _info = array, for ($i =0; $i <count ($public _info), $i + +) {for ($j = $i +1; $j <count ($public _info); $j + +) { if ($public _info[$j [' name '] = = $public _info[$i [' name ']) { $public _info[$i] [' value ']. = ', '. $public _info[$ j][' value ']; unset ($public _info[$j]);}}
Execution Result:
Array (7) {[0]=> Array (2) { ["name"]=> string (4) "Name" ["Value"]=> string (6) "Green Leaf"} [1]=> Array (2) { ["name"]=> string (5) "Phone" ["Value"]=> string (one) "13812341234"} [2]=> Array ( 2) { ["name"]=> string () "fileds_507[]" ["Value"]=> string (12) "I Am Green Leaf"} [3]=> Array (2) { ["Name"]=> string (fileds_508[] " [" Value "]=> string (6)" Hefei "} [4]=> Array (2) { ["Name"]=> string () "fileds_509[]" ["Value"]=> string (3) "Male"} [5]=> Array (2) { ["Name"]=> string (+) "fileds_510[]" ["Value"]=> string (13) "soccer, Baseball"} [7]=> Array (2) { ["Name"]=> string (fileds_511[] " [" Value "]=> string (+)" 2016-12-15t11:15 "}}
Summarize
The demand has been settled, as long as the array, we first think of in addition to the ready-made PHP method, is the loop, whether it is a foreach or for, the above example is my project processing of a custom form editing the small section of the process, I hope to be helpful to everyone's ideas.