PHP two-dimensional array to find the same key, splicing value

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

Listno the same values to reorganize into an array, the effect of the implementation:

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    ));

Listno the same values to reorganize into an array, the effect of the implementation:

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

    $arr = Array ("Shopid" = "C024", "Name" = "a", "listno" = 13343), Array ("shopid" = "C024", "Name" = "C", "Listno" and "=" 13343), Array ("shopid" = "C024", "Name" = "B", "Listno" = 1 3342), Array ("shopid" = "C024", "Name" = "D", "Listno" and "= 13" 342), Array ("shopid" = "C024", "Name" = "E", "listno" = 133 , Array ("shopid" = "C024", "Name" = "F", "listno" = 1334    6));    $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 [' N                        Ame ']) {$res [' name '] = Array ($res [' name '], $val [' name ']);                        } else {$res [' name '] [] = $val [' name '];        }}}}//Var_dump ($res);    } $result [] = $res; } var_dump ($result);
结果如下: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

Traversing the array, traversing the Listno as key, and then judged if isset the same key to the name of the merge operation, not directly written to the last array_merge to clear the array of key

  • 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.