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:
- 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.
- The result order within each [0], [1], [2]... groups doesn't matter as long as they are grouped together.
number
,title
, andtext
elements are linked, you can't.One
belongs to Number1
and TextOne
?$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.