Answer the question
In order to leave comments, you need to log in
How to compare php arrays?
there is a function that is called in a loop and makes an array of the form
array('a1'=>array(....),'a2'=>array(....) ...'aN'=>array(....));
Answer the question
In order to leave comments, you need to log in
The algorithm is as follows:
1. Iterate all 5 arrays, write the keys to a separate array.
2. Throw away all non-unique keys.
3. Iterate this separate array and inside the loop check for the presence of a key in each of the 5 arrays, if there is no key, add it with a default value.
function fill_array_diff(array $array1, array $array2) {
$diff = array_keys(array_diff_key($array1, $array2));
return array_merge($array2, array_fill_keys($diff, 0));
}
// Массив с которым сравниваем
$mainArray = ['a1' => 100, 'a2' => 200, 'a100' => 300, 'a200' => 400];
// Массив который сравниваем
$array = ['a1' => 1,'a2' => 2, 'a3' => 3];
$array = fill_array_diff($mainArray, $array);
var_export($array);
/*
array (
'a1' => 1,
'a2' => 2,
'a3' => 3,
'a100' => 0,
'a200' => 0,
)*/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question