Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this array structure which i am pulling from a database.

As you can see in each nested array there is a key UID. Im just wondering if it would be possible to split these up so that within the first nested array the two arrays with uid 22 are grouped together, and then the 23s are put together and so forth.

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 157
                    [uid] => 22
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9593
                    [size] => 1024x768
                )

            [1] => Array
                (
                    [id] => 158
                    [uid] => 22
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9593
                    [size] => 1280x1024
                )

            [2] => Array
                (
                    [id] => 166
                    [uid] => 23
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9764
                    [size] => 1024x768
                )

            [3] => Array
                (
                    [id] => 167
                    [uid] => 23
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] =>; 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9764
                    [size] => 1280x1024
                )

            [4] => Array
                (
                    [id] => 193
                    [uid] => 26
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 1660
                    [size] => 1024x768
                )

            [5] => Array
                (
                    [id] => 194
                    [uid] => 26
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 1660
                    [size] => 1280x1024
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 157
                    [uid] => 22
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9593
                    [size] => 1024x768
                )

            [1] => Array
                (
                    [id] => 158
                    [uid] => 22
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9593
                    [size] => 1280x1024
                )

            [2] => Array
                (
                    [id] => 166
                    [uid] => 23
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9764
                    [size] => 1024x768
                )

            [3] => Array
                (
                    [id] => 167
                    [uid] => 23
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 9764
                    [size] => 1280x1024
                )

            [4] => Array
                (
                    [id] => 200
                    [uid] => 26
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 1660
                    [size] => 2560x1440
                )

            [5] => Array
                (
                    [id] => 201
                    [uid] => 26
                    [name] => Lexmoto ZSX 125
                    [models] => #ZS125-48A#
                    [cmd] => 1
                    [lm] => 1
                    [pm] => 0
                    [active] => 1
                    [downloads] => 1660
                    [size] => 960x640
                )

        )
)

This is what i am aiming for:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                         (
                             [id] => 157
                             [uid] => 22
                             [name] => Lexmoto ZSX 125
                             [models] => #ZS125-48A#
                             [cmd] => 1
                             [lm] => 1
                             [pm] => 0
                             [active] => 1
                             [downloads] => 9593
                             [size] => 1024x768
                          )

                     [1] => Array
                          (
                              [id] => 158
                              [uid] => 22
                              [name] => Lexmoto ZSX 125
                              [models] => #ZS125-48A#
                              [cmd] => 1
                              [lm] => 1
                              [pm] => 0
                              [active] => 1
                              [downloads] => 9593
                              [size] => 1280x1024
                           )
                  )
              [1] => Array
                   (

                     [0] => Array
                          (
                               [id] => 166
                               [uid] => 23
                               [name] => Lexmoto ZSX 125
                               [models] => #ZS125-48A#
                               [cmd] => 1
                               [lm] => 1
                               [pm] => 0
                               [active] => 1
                               [downloads] => 9764
                               [size] => 1024x768
                          )

                      [1] => Array
                           (
                               [id] => 167
                               [uid] => 23
                               [name] => Lexmoto ZSX 125
                               [models] => #ZS125-48A#
                               [cmd] =>; 1
                               [lm] => 1
                               [pm] => 0
                               [active] => 1
                               [downloads] => 9764
                               [size] => 1280x1024
                            )
                    )
           )
 )

I dont know if this would be possible or not, i am stumped at how i could achieve this.

share|improve this question
 
I can't think of any php function to do it right now, maybe there are more elegant ways but personally I would do a foreach loop on the array and put them together as you want in a new one. Advice: for situations like this if you use var_export() instead of var_dump() or print_r() other users can copy the array easily to test/develop an answer for you xD –  aleation 19 hours ago
add comment

put on hold as off-topic by deceze, hek2mgl, Kristoffer S Hansen, Werner Henze, Julien Poulin 18 hours ago

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – Werner Henze, Julien Poulin
  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – deceze, hek2mgl, Kristoffer S Hansen
If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

You can simply merge the arrays and sort them: look at this code:

$arr1 = array(
                    array(array('id'=>'157', 'uid'=>'22', 'name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'), 
                                array('id'=>'158', 'uid'=>'22','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'166', 'uid'=>'23','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'167', 'uid'=>'23','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'193', 'uid'=>'26','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'194', 'uid'=>'26','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#')
                    ),
                    array(array('id'=>'157', 'uid'=>'22', 'name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'), 
                                array('id'=>'158', 'uid'=>'22','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'166', 'uid'=>'23','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'167', 'uid'=>'23','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'193', 'uid'=>'26','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#'),
                                array('id'=>'194', 'uid'=>'26','name'=>'Lexmoto ZSX 125', 'models'=>'#ZS125-48A#') 
                    )
            );
$arr2 = array_merge($arr1[0], $arr1[1]);        
asort($arr2);   
echo '<pre>';           
print_r($arr2);         
echo '</pre>';


and this is result:

Array
(
    [6] => Array
        (
            [id] => 157
            [uid] => 22
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [0] => Array
        (
            [id] => 157
            [uid] => 22
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [7] => Array
        (
            [id] => 158
            [uid] => 22
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [1] => Array
        (
            [id] => 158
            [uid] => 22
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [8] => Array
        (
            [id] => 166
            [uid] => 23
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [2] => Array
        (
            [id] => 166
            [uid] => 23
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [9] => Array
        (
            [id] => 167
            [uid] => 23
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [3] => Array
        (
            [id] => 167
            [uid] => 23
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [4] => Array
        (
            [id] => 193
            [uid] => 26
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [10] => Array
        (
            [id] => 193
            [uid] => 26
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [11] => Array
        (
            [id] => 194
            [uid] => 26
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

    [5] => Array
        (
            [id] => 194
            [uid] => 26
            [name] => Lexmoto ZSX 125
            [models] => #ZS125-48A#
        )

)
share|improve this answer
add comment

Iteration

  • Make yourself a clear picture of your source
  • Define how it should look like at the end
  • Compose a new array that fits your needs

Probably you'll have to do something similar like this.

// your source array
$source = array(
    array(
        array('id' => 1, 'uid' => '33'),
        array('id' => 2, 'uid' => '33'),
        array('id' => 3, 'uid' => '44'),
        array('id' => 4, 'uid' => '44'),
        array('id' => 5, 'uid' => '33'),
        array('id' => 6, 'uid' => '44')
    )
);

// will contain sorted source array's entries
$sorted = array();

Composition

Build your new array.

// compose your new array
foreach ($source as $group) {
    foreach ($group as $entry) {
        if ( ! array_key_exists($entry['uid'], $sorted))
            $sorted[$entry['uid']] = array();

        $sorted[$entry['uid']][] = $entry;
    }
}

Result

Check the output.

echo '<pre>';

// how your source looked like
var_dump($source);
echo '<hr>';

// how it looks now
var_dump($sorted);
echo '<hr>';

// additionally you may reset the index of the first array 
// (right now it uses the 'uids' as keys)
var_dump(array_merge($sorted));

Notes

Probably you'll have to adjust this script to your needs, a little bit.

share|improve this answer
add comment
$data = array(); //Your array with data

$uids = array(); //Mappings for uids
$new  = array(0 => array()); //Desired result.

foreach($data as $group){
  foreach($group as $piece){
    if (!in_array($piece['uid'], $uids)){
      $uids[] = $piece['uid'] ;
    }

    $new[0][array_search($piece['uid'], $uids)][] = $piece ;
  }
}

var_dump($new);
share|improve this answer
add comment

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