PHP:基于值的独立数组

时间:2018-08-14 13:26:04

标签: php filter

我有以下数组:

$raw = [
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'used'
    ],
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'used'
    ],
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'used'
    ],
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'used'
    ],
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'used'
    ],
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'new'
    ],
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'new'
    ],
    [
        'prop1' => 'somevalue',
        'prop2' => 'anothervalue',
        'path' => 'new'
    ],
];

我需要过滤该数组,最好使用filter_array来保持代码更整洁,我需要基于path属性进行过滤。所以我最终会得到:

$filtered = [
    'used' => [
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'used'
        ],
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'used'
        ],
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'used'
        ],
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'used'
        ],
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'used'
        ]
    ],
    'new' => [
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'new'
        ],
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'new'
        ],
        [
            'prop1' => 'somevalue',
            'prop2' => 'anothervalue',
            'path' => 'new'
        ]
    ]
];

1 个答案:

答案 0 :(得分:5)

您的代码缺少逗号,因此在语法上无效。 在下面的演示中查看更新后的更正代码。您只需要以下简单代码:

<?php
  $filtered = array();
  foreach ($raw as $item) {
    $filtered[$item["path"]][] = $item;
  }

输出

Array
(
    [used] => Array
        (
            [0] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => used
                )

            [1] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => used
                )

            [2] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => used
                )

            [3] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => used
                )

            [4] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => used
                )

        )

    [new] => Array
        (
            [0] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => new
                )

            [1] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => new
                )

            [2] => Array
                (
                    [prop1] => somevalue
                    [prop2] => anothervalue
                    [path] => new
                )

        )

)

演示: https://eval.in/1047516