Php two-dimensional array to find the same key and concatenate value

Source: Internet
Author: User
{Code...} reorganizes the same value of ListNo into an array. The effect is: {code ...}
array(    array(        "ShopID" => "C024",        "Name" => "a",        "ListNo" => 13343    ),    array(        "ShopID" => "C024",        "Name" => "c",        "ListNo" => 13343    ),    array(        "ShopID" => "C024",        "Name" => "b",        "ListNo" => 13342    ),    array(        "ShopID" => "C024",        "Name" => "d",        "ListNo" => 13342    ),    array(        "ShopID" => "C024",        "Name" => "e",        "ListNo" => 13346    ),    array(        "ShopID" => "C024",        "Name" => "f",        "ListNo" => 13346    ));

The same value of ListNo is reorganized into an array to achieve the following results:

array("ShopID" => "C024", "Name" => array("a", "c"), "ListNo" = 13343);

Reply content:
array(    array(        "ShopID" => "C024",        "Name" => "a",        "ListNo" => 13343    ),    array(        "ShopID" => "C024",        "Name" => "c",        "ListNo" => 13343    ),    array(        "ShopID" => "C024",        "Name" => "b",        "ListNo" => 13342    ),    array(        "ShopID" => "C024",        "Name" => "d",        "ListNo" => 13342    ),    array(        "ShopID" => "C024",        "Name" => "e",        "ListNo" => 13346    ),    array(        "ShopID" => "C024",        "Name" => "f",        "ListNo" => 13346    ));

The same value of ListNo is reorganized into an array to achieve the following results:

array("ShopID" => "C024", "Name" => array("a", "c"), "ListNo" = 13343);

    $arr = array(        array(            "ShopID" => "C024",            "Name"   => "a",            "ListNo" => 13343        ),        array(            "ShopID" => "C024",            "Name"   => "c",            "ListNo" => 13343        ),        array(            "ShopID" => "C024",            "Name"   => "b",            "ListNo" => 13342        ),        array(            "ShopID" => "C024",            "Name"   => "d",            "ListNo" => 13342        ),        array(            "ShopID" => "C024",            "Name"   => "e",            "ListNo" => 13346        ),        array(            "ShopID" => "C024",            "Name"   => "f",            "ListNo" => 13346        )    );    $lists = array_column($arr, 'ListNo');    $lists = array_flip(array_flip($lists));    $result = array();    foreach ($lists as $k => $v) {        $res = array();        foreach ($arr as $key => $val) {            if ($val['ListNo'] == $v) {                if (count($res) == 0) {                    $res = $val;                } else {                    if ($res['ShopID'] != $val['ShopID']) {                        if (!is_array($res['ShopID'])) {                            $res['ShopID'] = array($res['ShopID'], $val['ShopID']);                        } else {                            $res['ShopID'][] = $val['ShopID'];                        }                    }                    if ($res['Name'] != $val['Name']) {                        if (!is_array($res['Name'])) {                            $res['Name'] = array($res['Name'], $val['Name']);                        } else {                            $res['Name'][] = $val['Name'];                        }                    }                }            }//        var_dump($res);        }        $result[] = $res;    }    var_dump($result);
The result is as follows: array (size = 3)

0 =>

array (size=3)  'ShopID' => string 'C024' (length=4)  'Name' =>     array (size=2)      0 => string 'a' (length=1)      1 => string 'c' (length=1)  'ListNo' => int 13343

1 =>

array (size=3)  'ShopID' => string 'C024' (length=4)  'Name' =>     array (size=2)      0 => string 'b' (length=1)      1 => string 'd' (length=1)  'ListNo' => int 13342

2 =>

array (size=3)  'ShopID' =>     array (size=2)      0 => string 'C024' (length=4)      1 => string 'C025' (length=4)  'Name' =>     array (size=3)      0 => string 'e' (length=1)      1 => string 'f' (length=1)      2 => string 'g' (length=1)  'ListNo' => int 13346

Traverse the array, review the ListNo as the Key, and then determine whether to merge the Name if the isset has the same Key. If not, write the data directly. At the end, array_merge clears the Key of the array.

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.