如何在数组数组中找到相同的名称并在PHP中组合其值?

时间:2018-10-04 07:10:52

标签: php arrays

我有这种类型的数组结构。

Array
(
    [0] => Array
        (
            [name] => Name
            [value] => Ram
        )

    [1] => Array
        (
            [name] => Gender
            [value] => Male
        )

    [2] => Array
        (
            [name] => Age
            [value] => 25
        )

    [3] => Array
        (
            [name] => Color
            [value] => Red
        )

    [4] => Array
        (
            [name] => Color
            [value] => Yellow
        )

    [5] => Array
        (
            [name] => Game
            [value] => Cricket
        )

    [6] => Array
        (
            [name] => Game
            [value] => Football
        )

    .
    .
    .
)

如何查找包含在整个数组中的相同名称并将其值组合?在上面,ColorGame的名称是相同的,因此我需要在数组中使用Color和Game值。请注意,除了“颜色”和“游戏”,可能还有其他相似的名称。

Array
(
    [0] => Array
        (
            [name] => Name
            [value] => Ram
        )

    [1] => Array
        (
            [name] => Gender
            [value] => Male
        )

    [2] => Array
        (
            [name] => Age
            [value] => 25
        )

    [3] => Array
        (
            [name] => Color
            [value] => array( 'Red', 'Yellow' )
        )

    [4] => Array
        (
            [name] => Game
            [value] => array( 'Cricket', 'Football' )
        )
    .
    .
    .
)

1 个答案:

答案 0 :(得分:1)

尝试以下代码。并检查是否对您有帮助。 注意:当您在此处提出问题时,请日后提供参考,以供将来参考。在这个问题中,您似乎并没有尝试解决它。​​

<?php
    // example code

    $newarray = [
            [
                "name" => "Name",
                "value" => "Ram"
            ],
            [
                "name" => "Gender",
                "value" => "Male"
            ],
            [
                "name" => "Age",
                "value" => 25
            ],
            [
                "name" => "Color",
                "value" => "Red"
            ],
            [
                "name" => "Color",
                "value" => "Yellow"
            ],
        ];

//make an array of combined values using value of name as key of that array
    $keyArray = [];
    foreach($newarray as $k=>$v)
    {
        if(array_key_exists($v['name'],$keyArray))
        {
            $valueArray = [];
            if(is_array($keyArray[$v['name']]))
            {
                $valueArray = $keyArray[$v['name']];
                $valueArray[] = $v['value'];
                $keyArray[$v['name']] = $valueArray;
            }
            else
            {
                $valueArray[] = $keyArray[$v['name']];
                $valueArray[] = $v['value'];
                $keyArray[$v['name']] = $valueArray;
            }
        }
        else
        {
            $keyArray[$v['name']] = $v['value'];
        }
    }
//now loop through the key values and make key as name and value as value and push into a wrapper array
    $finalArray = [];
    foreach ($keyArray as $k=>$v)
    {
        $finalArray[] = [
                "name"=>$k,
                "value"=>$v
            ];
    }

    print_r($finalArray);