繁体   English   中英

PHP:基于值的独立数组

[英]PHP: Separate Array based on a value

我有以下数组:

$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'
        ]
    ]
];

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

<?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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM