【问题标题】:Recursively group arrays on multiple keys in PHP在PHP中的多个键上递归分组数组
【发布时间】:2020-11-26 14:32:47
【问题描述】:

我在尝试在 php 中对具有多个值的数组进行分组时遇到问题...

给定一个数组:

    Array
    (
        [0] => Array
            (
                [displayOrder] => 1
                [bannerType] => STC
                [targetResult] => blob:https://localhost/7d00621b-471f-4613-bfa4-ab87d0b1f70a
            )

    [1] => Array
        (
            [displayOrder] => 2
            [bannerType] => SLD
            [targetResult] => blob:https://localhost/da3e95f9-2bbb-4547-aa04-9b3d01fed1c9
        )

    [2] => Array
        (
            [displayOrder] => 2
            [bannerType] => SLD
            [targetResult] => blob:https://localhost/2a01044e-e958-44e0-ae0c-cc8136665941
        )

    [3] => Array
        (
            [displayOrder] => 2
            [bannerType] => SLD
            [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
        )

    [4] => Array
        (
            [displayOrder] => 3
            [bannerType] => GRD
            [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
        )

    [5] => Array
        (
            [displayOrder] => 3
            [bannerType] => GRD
            [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
        )

    [6] => Array
        (
            [displayOrder] => 4
            [bannerType] => SLD
            [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
        )

      )

我想得到这样的..我想使用 displayOrder 对上面的数组进行分组,但用bannerType动态索引它


    Array
    (
    [STC] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 1
                    [bannerType] => STC
                    [targetResult] => blob:https://localhost/7d00621b-471f-4613-bfa4-ab87d0b1f70a
                )

        )

    [SLD] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/da3e95f9-2bbb-4547-aa04-9b3d01fed1c9
                )

            [1] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/2a01044e-e958-44e0-ae0c-cc8136665941
                )

            [2] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

    [GRD] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 3
                    [bannerType] => GRD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

            [1] => Array
                (
                    [displayOrder] => 3
                    [bannerType] => GRD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

    [SLD] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 4
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

        )

我已经为第一个给定数组尝试了一些代码

           foreach($array as $key => $val){
                $output[$val['displayOrder']][] = $val;
            }

结果是

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 1
                    [bannerType] => STC
                    [targetResult] => blob:https://localhost/7d00621b-471f-4613-bfa4-ab87d0b1f70a
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/da3e95f9-2bbb-4547-aa04-9b3d01fed1c9
                )

            [1] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/2a01044e-e958-44e0-ae0c-cc8136665941
                )

            [2] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 3
                    [bannerType] => GRD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

            [1] => Array
                (
                    [displayOrder] => 3
                    [bannerType] => GRD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 4
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

)

如果有人可以帮助我,将不胜感激! 对不起我的英语..

【问题讨论】:

  • 您是否有权访问最初生成数组的代码?那将是进行更改的合乎逻辑的地方。
  • 不..我必须在第一个数组之后更改它
  • 我在这里看不到任何需要实际递归的地方。
  • “我已经尝试了一些代码” - 和?结果如何?
  • 请至少以var_export 的形式提供您的输入数组 - 然后我们只需要复制代码即可使用您所拥有的,而无需修改您拥有的当前首先显示为一个可行的数组表示。

标签: php arrays count grouping field


【解决方案1】:

我希望这是您的问题的解决方案。

$db = Array
    (
        0 => Array
            (
                'displayOrder' => '1',
                'bannerType' => 'STC',
                'targetResult' => 'blob:https://localhost/7d00621b-471f-4613-bfa4-ab87d0b1f70a',
            ),
    1 => Array
        (
            'displayOrder' => '2',
            'bannerType' => 'SLD',
            'targetResult' => 'blob:https://localhost/da3e95f9-2bbb-4547-aa04-9b3d01fed1c9',
        ),
    2 => Array
        (
            'displayOrder' => '2',
            'bannerType' => 'SLD',
            'targetResult' => 'blob:https://localhost/2a01044e-e958-44e0-ae0c-cc8136665941',
        ),
    3 => Array
        (
            'displayOrder' => '2',
            'bannerType' => 'SLD',
            'targetResult' => 'blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770',
        ),
    4 => Array
        (
            'displayOrder' => '3',
            'bannerType' => 'GRD',
            'targetResult' => 'blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770',
        ),
    5 => Array
        (
            'displayOrder' => '3',
            'bannerType' => 'GRD',
            'targetResult' => 'blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770',
        ),
    6 => Array
        (
            'displayOrder' => '4',
            'bannerType' => 'SLD',
            'targetResult' => 'blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770',
        ),
  );

解决方案:

//Distinct Banner Type
$bannerType=array_unique(array_column($db, 'bannerType'));
$newArray=array();
foreach ($bannerType as $value) {
    //Creating new array with distinct banner type
    $newArray[$value]=array();
}
foreach ($db as $value) {
    $newArray[$value['bannerType']][]=$value;
}
print_r($newArray);

输出:

Array
(
    [STC] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 1
                    [bannerType] => STC
                    [targetResult] => blob:https://localhost/7d00621b-471f-4613-bfa4-ab87d0b1f70a
                )

        )

    [SLD] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/da3e95f9-2bbb-4547-aa04-9b3d01fed1c9
                )

            [1] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/2a01044e-e958-44e0-ae0c-cc8136665941
                )

            [2] => Array
                (
                    [displayOrder] => 2
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

            [3] => Array
                (
                    [displayOrder] => 4
                    [bannerType] => SLD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

    [GRD] => Array
        (
            [0] => Array
                (
                    [displayOrder] => 3
                    [bannerType] => GRD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

            [1] => Array
                (
                    [displayOrder] => 3
                    [bannerType] => GRD
                    [targetResult] => blob:https://localhost/71817ae7-e887-4b0f-982b-a6877fac9770
                )

        )

)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多