0

I am trying to group elements of variable array called $list with subarrays with random subarray order like:

$list = array(
    1 => array('number' => 1),
    2 => array('title' => 'Title One'),
    3 => array('text' => 'Text One'),

    4 => array('title' => 'Title Two'),
    5 => array('number' => 2),
    6 => array('text' => 'Text Two'),

    7 => array('text' => 'Text Three'),
    8 => array('title' => 'Title Three'),
    9 => array('number' => 3),
);

How can I group them into

Array
(
    [0] => Array
        (
            [number] => 1
            [title] => Title One
            [text] => Text One
        )

    [1] => Array
        (
            [title] => Title Two
            [number] => 2
            [text] => Text Two
        )

    [2] => Array
        (
            [text] => Text Three
            [title] => Title Three
            [number] => 3
        )

)

Important Note:

  1. The subarray keys can be any string, not just 'title', 'text', 'number'. Meaning I can have 1 or more elements, it's not predefined to 3 ('title', 'text', 'number') in this case.
  2. The result order within each [0], [1], [2]... groups doesn't matter as long as they are grouped together.
22
  • 3
    go back to the source and return meaningful data Commented Feb 24, 2014 at 22:49
  • 2
    If it's in completely random order, then absent some dictionary to tell it which number, title, and text elements are linked, you can't. Commented Feb 24, 2014 at 22:49
  • 2
    How is the program supposed to know that Title One belongs to Number 1 and Text One? Commented Feb 24, 2014 at 22:53
  • 2
    sigh, can i have my 10 minutes back please? Commented Feb 24, 2014 at 23:07
  • 2
    @user702300 I understand, but as you can see, what you've presented has yielded nothing but everybody telling you that you're SoL. So, either you need to back up and focus on what you are starting with, or, well, continue to be SoL. Because there is a clear disconnect between what you are showing that you're starting with and the arrays you have arrived at. If you are able to get that first $list then putting them into $results like you want is easy. But then you say it's sometimes in the order of the 2nd $list and that's not consistent with the source you listed. Commented Feb 25, 2014 at 1:00

2 Answers 2

0

Try this out ,

array_chunk($list,3,true);

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

Comments

0

You can do the following:

$keyCount = array();
$finalArray = array();

foreach($list as $value) {
    $temp = key($value);
    $currentKey = $temp[0];
    $count = isset($keyCount[$currentKey]) ? $keyCount[$currentKey]: 0;
    $keyCount[$currentKey] = $count + 1;
    $finalArray[$count] = isset($finalArray[$count]) ? $finalArray[$count] : array();
    $finalArray[$count][$currentKey] = $value[$currentKey];
}

Comments

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.