1

I have a multidimensional-array which looks like this:

$array = (
    [0] => array (
        ['WS'] => array(
             [id] => 2,
             [name] => 'hello'
             )
        )
    ), 
    [1] => array (
        ['SS'] => array(
             [id] => 1,
             [name] => 'hello2'
             )
        )
    ),
    [2] => array (
        ['WS'] => array(
             [id] => 5,
             [name] => 'helloAGAIN'
             )
        )
)

As you can see, $array[0] and $array[2] have the same key [WS]. I need a function to find those "same keys". Afterthat I would merge these two arrays into one. f.e.

$array =
(
    [0] => array 
        (
            ['WS'] => array
                (
                     [0] => array
                         (
                             [id] => 2,
                             [name] => 'hello'
                         ),
                     [1] => array
                         (
                            [id] => 5,
                            [name] => 'helloAGAIN'
                         )
                )
        ),
    [1] => array 
         (
             ['SS'] => array
                 (
                     [0] => array
                         (
                              [id] => 1,
                              [name] => 'hello2'
                         )
                 )
         )
    )

Hope you guys understand my problem. greets

1
  • 2
    Why not just iterate over it and rebuild your array? one pass and some kind of key lookup array should cover it. Commented Jan 12, 2012 at 17:51

3 Answers 3

8
function group_by_key ($array) {
  $result = array();
  foreach ($array as $sub) {
    foreach ($sub as $k => $v) {
      $result[$k][] = $v;
    }
  }
  return $result;
}

See it working

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Its quite a good solution for me. Easy to understand and works well.
1

you can just loop through the array and delete matching elements

    $multiArray = array('0' => etc etc);
    $matches = array();

    foreach(multiArray as $key => $val)
    {
       $keyValToCheck = key($val);

       if(!in_array($keyValToCheck, $matches))
       {
          $matches[] = $keyValToCheck; // add value to array matches

          // remove item from array because match has been found
          unset($multiArray[$key][$keyValToCheck]);
       }
    }

Comments

1

You could simply eliminate the first level of your array and you would end up with something like this:

$array = (
    ['WS'] => array(
        [0] => array(
                [id] => 2,
                [name] => 'hello'
        ),
        [1] => array(
               [id] => 5,
               [name] => 'helloAGAIN'
        )
    ),
    ['SS'] => array(
        [0] => array(
              [id] => 1,
              [name] => 'hello2'
        )
    )
)

That way you can add things to your array like this:

$array['WS'][] = array();

1 Comment

thx a lot :) your structure of the array has led me to a solution of my problem. My thoughts about building a array was far to complicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.