Reputation: 123
I need to merge these 3 arrays so that the output is a 2d array holding all values from the other arrays.
Like this:
Array 1:
$winners = array(
[1] => '20',
[5] => '2'
);
Array 2:
$losers = array(
[1] => '5',
[2] => '1',
);
Array 3
$equal = array(
[1] => '1',
[2] => '2',
);
Result:
$result = array(
[1] => array(
[0] => '20', //First array
[1] => '5', //Second array
[2] => '1' //Third array
),
[2] => array(
[0] => '0', //First array (Its not in the first array, so result 0)
[1] => '1',
[2] => '2'
),
[5] => array(
[0] => '2',
[1] => '0',
[2] => '0'
),
);
Ive did some searching on the internet, but playing around with array_merge_recursive() and array_merge() did not give me the desired result. I know there are a lot of topics about merging arrays, but i just cannot find any topic doing the same thing as i do.
Upvotes: 1
Views: 94
Reputation: 18290
Okay, I think this is the ticket! This function will combine all arrays passed into it such that there is a key in the result for every key in the args, and the value of that key is an array with n values where n is the number of passed-in arrays. If the key for the given array exists in the corresponding passed-in array (that is, the array at the same index in the args as the index in the results sub-array), then it uses that value, else zero.
It helped me to think about the keys in the passed-in array as 'player ids', but that name could be considered arbitrary.
function combine_scores() {
$result = array();
$player_ids = array();
$num_arrays = func_num_args();
// get list of all 'players'
foreach(func_get_args() as $input_array) {
foreach($input_array as $key=>$val) {
$player_ids[$key] = true; // memory optimization compared to $player_ids[] = $key;
}
}
// setup the final array
foreach(array_keys($player_ids) as $player_id) {
foreach(func_get_args() as $input_array) {
$result[$player_id][] = array_key_exists($key, $input_array) ? $input_array[$player_id] : 0;
}
}
return $result;
}
Using it would look like this:
$winners = array('5'=>1);
$losers = array('2'=>3);
$result = combine_scores($winners, $losers);
Upvotes: 1
Reputation: 7769
Try this, but it does not add the zeros for the undefined index like in index 5, what you can do instead of wasting memory for the default value. You can write while using it:
if(!isset($all[5][1])) echo '0';
Here's the code:
$all = array($winners, $losers, $equals);
$output = array();
foreach($all as $subArray){
foreach($subArray as $key => $value){
$output[$key][] = $value;
}
}
Upvotes: 1
Reputation: 4748
array_merge_recursive()
won't work for your purposes here because it will not empty fill with 0 if not present in the other arrays.
Try something like this:
$result = array();
// Get all distinct keys from all arrays.
// This is needed to make sure the 0 fill works.
$distinct_keys = array_unique(array_merge(
array_keys($array1),
array_keys($array2),
array_keys($array3)
));
// Loop through each distinct key.
// If the index exists in each array, add it, otherwise, add '0'
foreach ($distinct_keys as $key) {
$result[$key] = array();
$result[$key][] = isset($array1[$key]) ? $array1[$key] : '0';
$result[$key][] = isset($array2[$key]) ? $array2[$key] : '0';
$result[$key][] = isset($array3[$key]) ? $array3[$key] : '0';
}
Upvotes: 2