1

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
AymDev
  • 6,626
  • 4
  • 29
  • 52
  • Without knowing where does the added data comes from it is difficult to help you. For sorting: [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – AymDev Aug 20 '18 at 10:17

1 Answers1

0

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,
//];
Rafael
  • 1,495
  • 1
  • 14
  • 25