The code shared in this article is a php algorithm that combines two-dimensional arrays based on key values and duplicates them. it is very practical. if you need a small partner, you can refer to the following requirements.
The following two-dimensional array is available:
Array (8) {[0] => array (2) {["name"] => string (4) "name" ["value"] => string (6) "Qingye"} [1] => array (2) {["name"] => string (5) "phone" ["value"] => string (11) "13812341234"} [2] => array (2) {["name"] => string (12) "fileds_507 []" ["value"] => string (12) "I am Qingye"} [3] => array (2) {["name"] => string (12) "fileds_508 []" ["value"] => string (6) "Hefei"} [4] => array (2) {["name"] => string (12) "fileds_509 []" ["value"] => string (3) "male"} [5] => array (2) {["name"] => string (12) "fileds_510 []" ["value"] => string (6) "Soccer"} [6] => array (2) {["name"] => string (12) "fileds_510 []" ["value"] => string (6) "baseball"} [7] => array (2) {["name"] => string (12) "fileds_511 []" ["value"] => string (16) "2016-12-15T11: 15 "}}
You need to merge the values of the arrays with the same name and value to form a new array.
For example, the two-dimensional arrays whose name is fileds_510 in the above code should be merged into an array whose value is football and baseball.
Ideas
When it comes to arrays, we first think of loops in PHP, so here it is obviously better to use for loop processing. it is similar to bubble sorting, and it is better to compare them one by one.
Code
The code is as follows:
$ Public_info = array; for ($ I = 0; $ I
Execution result:
Array (7) {[0] => array (2) {["name"] => string (4) "name" ["value"] => string (6) "Qingye"} [1] => array (2) {["name"] => string (5) "phone" ["value"] => string (11) "13812341234"} [2] => array (2) {["name"] => string (12) "fileds_507 []" ["value"] => string (12) "I am Qingye"} [3] => array (2) {["name"] => string (12) "fileds_508 []" ["value"] => string (6) "Hefei"} [4] => array (2) {["name"] => string (12) "fileds_509 []" ["value"] => string (3) "male"} [5] => array (2) {["name"] => string (12) "fileds_510 []" ["value"] => string (13) "soccer, baseball "} [7] => array (2) {[" name "] => string (12)" fileds_511 [] "[" value "] => string (16) "2016-12-15T11: 15 "}}
Summary
The requirement has been fixed. as long as an array is encountered, the first thing we think of is a loop in addition to the ready-made PHP method, whether it is foreach or, the above example is a short process of custom form editing in my project. I hope it will be helpful for your ideas.
For more articles about the PHP two-dimensional array de-duplication algorithm, refer to the PHP Chinese network!