i have two arrays and want to make them same at the length and keys like this:
Array
(
[1399-1-27] => 2
[1399-1-26] => 6
[1399-1-25] => 3
[1399-1-24] => 3
[1399-1-23] => 2
)
Array
(
[1399-1-27] => 3.6666666666667
[1399-1-26] => 4
[1399-1-25] => 2.6666666666667
)
and what i want is the second array most convert to something like this:
Array
(
[1399-1-27] => 3.6666666666667
[1399-1-26] => 4
[1399-1-25] => 2.6666666666667
[1399-1-24] => 0
[1399-1-23] => 0
)
does php have functions for this purpose or any solutions?
you could set the values of the first array to zero and then merge the two to achieve your result.
<?php
$array1= [
'1399-1-27' => 2,
'1399-1-26' => 6,
'1399-1-25' => 3,
'1399-1-24' => 3,
'1399-1-23' => 2
];
$array2 = [
'1399-1-27' => 3.6666666666667,
'1399-1-26' => 4,
'1399-1-25' => 2.6666666666667
];
$array1_0 = array_fill_keys(array_keys($array1), 0);
$result = array_merge($array1_0, $array2);
print_r($result);
?>
Result
Array
(
[1399-1-27] => 3.6666666666667
[1399-1-26] => 4
[1399-1-25] => 2.6666666666667
[1399-1-24] => 0
[1399-1-23] => 0
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.