开发者

Output arrays within an array grouped by a common index

I have an array, seen below. My desired output is to group by store, and then concatenate (I think?) all of the related quantities and denominations into those arrays. I've shown what the desired output would be.

CURRENT ARRAY

Array
(
    [denomination] => Array
        (
            [0] => 25
            [1] => 50
            [2] => 100
            [3] => 200
        )

    [quantity] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 2
            [3] => 4
        )

    [store] => Array
        (
            [0] => candy store
            [1] => candy store
            [2] => book store
            [3] => candy store
        )

)

DESIRED OUTPUT

Array
(
    [candy store] => Array
        (
            [0] => Array
                (
                    [denomination] => Array
                        (
                            [0] => 25
                            [1] => 50
                            [2] => 200
                        )

                )

            [1] => Array
                (
                    [quantity] => Array
                        (
                            [0] => 1
                            [1] => 1
                            [2] => 4
                        )
                )

        )

    [book store] => Array
        (
            [0] => Array
                (
                    [denomination] => Array
                        (
                            [0] => 100
                        )

                )

            [1] =&g开发者_JS百科t; Array
                (
                    [quantity] => Array
                        (
                            [0] => 2
                        )
                )
        )

)


$result = array();
foreach ($array['store'] as $index => $type) {
  $result[$type]['denomination'][] = $array['demoniation'][$index];
  $result[$type]['quantity'][] = $array['quantity'][$index];
}

This is not exactly, what you specified as "desired output", but I don't see a reason, why one should put the denomination- and quantity-arrays into additional arrays.

However, if this has any reason, you can get it similar

$result = array();
foreach ($array['store'] as $index => $type) {
  $result[$type][0]['denomination'][] = $array['demoniation'][$index];
  $result[$type][1]['quantity'][] = $array['quantity'][$index];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜