PHP two-dimensional array de-weight algorithm sample code

Source: Internet
Author: User
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" }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.