Home > Backend Development > PHP Tutorial > php二维数组找出相同的key,拼接value

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

WBOY
Release: 2016-06-06 20:19:22
Original
1708 people have browsed it

<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
    )
);</code>
Copy after login
Copy after login

ListNo相同的值重新组织成一个数组,实现的效果:

<code>array("ShopID" => "C024", "Name" => array("a", "c"), "ListNo" = 13343);</code>
Copy after login
Copy after login

回复内容:

<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
    )
);</code>
Copy after login
Copy after login

ListNo相同的值重新组织成一个数组,实现的效果:

<code>array("ShopID" => "C024", "Name" => array("a", "c"), "ListNo" = 13343);</code>
Copy after login
Copy after login

<code>    $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);</code>
Copy after login
<code>
结果如下:
array (size=3)</code>
Copy after login

0 =>

<code>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</code>
Copy after login

1 =>

<code>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</code>
Copy after login

2 =>

<code>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
</code>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template