how to add array to be like this in php?
before:
'a' => 3, 'b' => 9
after:
'a' => 3, 'b' => 9, 'c' => 7, 'd' => 1
and sorting it with ascending
'd' => 1, 'a' => 3, 'c' => 7, 'b' => 9
how to add array to be like this in php?
before:
'a' => 3, 'b' => 9
after:
'a' => 3, 'b' => 9, 'c' => 7, 'd' => 1
and sorting it with ascending
'd' => 1, 'a' => 3, 'c' => 7, 'b' => 9
Assuming you would have the first array and the second like this:
$firstArray = ['a' => 3, 'b' => 9];
$secondArray = ['c' => 7, 'd' => 1];
PHP allows you to concatenate the results by using the plus symbol +:
$test = $firstArray + $secondArray;
//$test = [
// 'a' => 3,
// 'b' => 9,
// 'c' => 7,
// 'd' => 1,
//];
And after that, to sort by values:
asort($test);
//$test = [
// 'd' => 1,
// 'a' => 3,
// 'c' => 7,
// 'b' => 9,
//];