-1

I have a multiple arrays which I'd like to put into a single array in order to sort it:

$weight = array($weight);
$dev = array_combine($missing, $weight);

echo "<pre>"; 
print_r($dev); 
echo "</pre>";

Output:

Array (
  [angular] => 2
) 
Array (
  [android sdk] => 3
) Array (
  [application] => 1
)

Now how do I turn the array above into this?

Array (
    [android sdk] => 3
    [angular] => 2
    [application] => 1 )

I've tried the below from a solution that I've found on this site, but it returns NULL:

$weight = array($weight);
$dev = array_combine($missing, $weight);
$result = call_user_func_array("array_merge", $dev);

echo "<pre>"; 
print_r($result); 
echo "</pre>";

EDIT

Here is my $missing array, some arrays are empty because a match hasn't been found against some keywords:

Array
(
)
Array
(
    [0] => angular
)
Array
(
    [0] => android sdk
)
Array
(
    [0] => application
)
Array
(
)

Here are the value from $weight:

3 2 3 1 3

How can I get this?

Array (
    [android sdk] => 3
    [angular] => 2
    [application] => 1 )
Seb
  • 83
  • 1
  • 10
  • https://secure.php.net/manual/en/function.array-merge.php example 3 – treyBake Jul 26 '18 at 11:55
  • Try the `+` operator instead of `array_combine` function http://php.net/manual/en/language.operators.array.php – Raymond Nijland Jul 26 '18 at 11:56
  • 1
    `array_combine(['android sdk', 'angular', 'application'], [3, 2, 1]);` works fine. Your doing something wrong. Since we don't know what is in your variable, we can't help you on that point, but `$weight = array($weight);` is dubious. – Blackhole Jul 26 '18 at 11:56
  • How can one call of `print_r` output two arrays? – Nico Haase Jul 26 '18 at 11:58

2 Answers2

3

use array_merge:

$array1 = [1,2,3];
$array2 = [4,5,6];

$result = array_merge($array1, $array2);
print_r($result);

results in:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Doğan Uçar
  • 166
  • 3
  • 15
2

You can use array_merge function.

Therefore, the code will be

array_merge($array1, $array2);
Noor A Shuvo
  • 2,639
  • 3
  • 23
  • 48
  • Thanks for your answer. I've edited my question. I get the following with array_merge: Array ( [0] => 3 ) Array ( [0] => angular [1] => 2 ) Array ( [0] => android sdk [1] => 3 ) Array ( [0] => application [1] => 1 ) Array ( [0] => 3 ) – Seb Jul 26 '18 at 13:35
  • Thanks Noor A Shuvo. array_merge didn't work for me, in the end, I posted my entire code here [link](https://stackoverflow.com/questions/51547406/sorting-array-value-in-descending-order) – Seb Jul 27 '18 at 10:59