PHP two-dimensional array de-weight algorithm

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

Related Article

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.