This article to share the code is the PHP implementation of the two-dimensional array based on key values to merge and to repeat the algorithm, very practical, the need for small partners can refer to,
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"]=> stri Ng (6) "Green Leaf"} [1]=> Array (2) {["Name"]=> string (5) "Phone" ["Value"]=> string (one) "13812341234"} [2]=> ar Ray (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 (1 3) "Football, Baseball"} [7]=> Array (2) {["Name"]=> string ("fileds_511[]" ["Value"]=> string () "2016-12-15t11:15" }}