php二维数组找出相同的key,拼接value

发布于 2022-09-02 01:12:07 字数 794 浏览 17 评论 0

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相同的值重新组织成一个数组,实现的效果:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2022-09-09 01:12:07
    $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);

结果如下:
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
猫瑾少女 2022-09-09 01:12:07

遍历数组,遍历时把ListNo作为Key,然后判断下如果isset同样的Key就对Name做合并操作,没有就直接写入,到最后array_merge一下清除掉数组的Key

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文