Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This is my 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"
    )

)

I want to group arrays with same "id" value. This should be the output:

[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"
        )

    )

)

I tryed with tens of foreach statements but there's no way for me to get arrays with same "id" inside the same array. Is it probably related with the fact that it's a multidimensional array? Should i use 2 nested foreach to get the result?

Sorry, I did a lot of confusion. I need a bed. I've updated my "i want this output" part.

share|improve this question
up vote 1 down vote accepted

Code:

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

Result:

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
                        )

                )

        )

)

if you don't want to preserve keys, just call array_values before return.

share|improve this answer
    
Basically i was trying with tens of similar functions with no success. Thanks to your "Result" example i understand where the problem was. I was not passing result inside an Array(). I was always passing 3 separate arrays Array(1) Array(2) Array(3) instead of Array( Array(1) Array(2) Array(3) ). Thank you CertaiN. – user1274113 May 15 '13 at 8:47

Use id for key of new array.

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

share|improve this answer

If you wanted to use a foreach:

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

Now $return contains them groped by ID, where keys 1 and 2 are your IDs

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

You can then access your groups like this:

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
}
share|improve this answer
    
Yes, i was using the same approach but all i get is many buggy arrays (there's some strange logic) but most important all elements with same ID always stay in their own array. Basically [0] => (mess), [1] => (mess), [2] = (mess). I still have 3 keys. – user1274113 May 14 '13 at 15:47
    
Sorry. I got it. Your code is working but it's not what i was looking for. Sadly i did a mistake posting my "i want this output" part. – user1274113 May 14 '13 at 16:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.