0

这是我的阵列。

Array
(
    [id] => 1
    [color] => "White"
    [something] => Array
    (
        [country] => "France"
        [city] => "Paris"
    )

)
Array
(
    [id] => 2
    [color] => "Black"
    [something] => Array
    (
        [country] => "Germany"
        [city] => "Berlin"
    )

)
Array
(
    [id] => 2
    [color] => "Red"
    [something] => Array
    (
        [country] => "Russia"
        [city] => "Moscow"
    )

)

我想对具有相同id值的数组进行分组。这应该是输出:

[0] => Array
(
    [0] => Array
    (
        [id] => 1
        [color] => "White"
        [something] => Array
        (
                [country] => "France"
                [city] => "Paris"
        )

    )

)
[1] => Array
(

    [0] => Array
    (
        [id] => 2
        [color] => "Black"
        [something] => Array
        (
            [country] => "Germany"
            [city] => "Berlin"
        )

    )
    [1] => Array
    (

        [id] => 2
        [color] => "Red"
        [something] => Array
        (
            [country] => "Russia"
            [city] => "Moscow"
        )

    )

)

我尝试了数十条foreach语句,但我无法在同一个数组中获得id相同的数组。它可能与它是一个多维数组的事实有关吗?我应该使用 2 嵌套foreach来获得结果吗?

4

3 回答 3

1

代码:

<?php

$arr = array(
    array(
        'id'    => 1,
        'color' => 'white',
        'something' => array(
            'country' => 'France',
            'city'    => 'Paris',
        ),
    ),
    array(
        'id'    => 2,
        'color' => 'Black',
        'something' => array(
            'country' => 'Germany',
            'city'    => 'Berlin',
        ),
    ),
    array(
        'id'    => 2,
        'color' => 'Red',
        'something' => array(
            'country' => 'Russia',
            'city'    => 'Moscow',
        ),
    ),
);


function groupify($arr) {
    $new = array();
    foreach ($arr as $item) {
        if (!isset($new[$item['id']])) {
            $new[$item['id']] = array();
        }
        $new[$item['id']][] = $item;
    }
    return $new;
}

print_r(groupify($arr));

结果:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [color] => white
                    [something] => Array
                        (
                            [country] => France
                            [city] => Paris
                        )

                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [color] => Black
                    [something] => Array
                        (
                            [country] => Germany
                            [city] => Berlin
                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [color] => Red
                    [something] => Array
                        (
                            [country] => Russia
                            [city] => Moscow
                        )

                )

        )

)

如果您不想保留密钥,只需调用array_valuesbefore return

于 2013-05-14T16:05:55.277 回答
1

使用 id 作为新数组的键。

$a[$array[id]][] = $array;

于 2013-05-14T15:19:13.470 回答
0

如果你想使用 foreach:

<?php
$return = array();
foreach($array as $key => $innerArray) {
    $return[$innerArray['id']][]= $innerArray;
}

现在 $return 包含按 ID 摸索的它们,其中键 1 和 2 是您的 ID

array(
    1 => array(
        array(/** **/) 
    ),
    2 => array(
        array(/** **/),
        array(/** **/) 
    );
);

然后,您可以像这样访问您的组:

foreach($return as $key => $groupArray) {
    // you have the groups here

    foreach($groupArray as $id => $singleArray) {
       // singleArray contains your id, colour etc
    }
}

foreach($return[1] as $groupOne) {
    // all arrays with id = 1
}
于 2013-05-14T15:27:09.253 回答