I've been wrapping my head around this for a couple of days...
I have several arrays that need to be sort of merged into a single array. The order in which they merge is of great importance and is simply the order in which they appear in the global array (like the example below):
$input1 = array(
array(
'context' => 'aa', 'id' => 1, 'view' => 1, 'update' => 1,
),
array(
'context' => 'bb', 'id' => 2, 'view' => 0, 'update' => 0,
)
);
$input2 = array(
array(
'context' => 'cc', 'id' => 3, 'view' => 0, 'update' => 1,
),
array(
'context' => 'dd', 'id' => 4, 'view' => 0, 'update' => 0,
),
array(
'context' => 'ee', 'id' => 5, 'view' => 1, 'update' => 0,
)
);
$input3 = array(
array(
'context' => 'ff', 'id' => 6, 'view' => 1, 'update' => 1,
),
array(
'context' => 'gg', 'id' => 7, 'view' => 1, 'update' => 0,
),
);
$global = array($input1, $input2, $input3);
Each input array itself consists of several subarrays that are of equal structure; see http://pastebin.com/fQMUjUpB for an example. This pastebin code also includes the desired output. The output array should contain:
- a single level array
- a tree-like passthrough upon "merging the next input array", viz. every possible cross-combination of subarrays should be made during a merge between two input arrays
- the key of each a combination should be generated as a concatenated string of the corresponding
contextandidelements (glued with a plus) joined together with an ampersand (&); e.g:context1+id1&context2+id2 - For the next merge the previous resulting array should be used in order for the example from above becomes
context1+id1&context2+id2&context3+id3 - The resulting
viewandupdateelements are calculated by simply multiplying their corresponding values during merge.
$output = array(
'aa+1&cc+3&ff+6' => array('view' => 0, 'update' => 1),
'aa+1&cc+3&gg+7' => array('view' => 0, 'update' => 0),
'aa+1&dd+4&ff+6' => array('view' => 0, 'update' => 0),
'aa+1&dd+4&gg+7' => array(...),
'aa+1&ee+5&ff+6' => array(...),
'aa+1&ee+5&gg+7' => array(...),
'bb+2&cc+3&ff+6' => array(...),
'bb+2&cc+3&gg+7' => array(...),
'bb+2&dd+4&ff+6' => array(...),
'bb+2&dd+4&gg+7' => array(...),
'bb+2&ee+5&ff+6' => array(...),
'bb+2&ee+5&gg+7' => array(...)
);
How can this be accomplished when looping over $global?
I may have expressed myself quite vaguely (it's really hard to explain!), but hopefully it becomes more clear when you take a look at the pastebin code...
Any help would be greatly appreciated!
contextandidof a subarray, combine them with a plus sign and then concatenate it with the (existing) key of the resulting output array by using an ampersand.viewit would be$aa['view'] * $cc['view'] * $gg['view']