0

I have an array that is generated based on data retrieved from database and looks like this:

Array ( [0] => ["Tablet","Budget"] [1] => ["Phone","VPN"] )

or

Array ( [0] => ["Tablet","Budget", "Cloud"] [1] => ["Phone","VPN"] )

or

Array ( [0] => ["Tablet","Budget"] [1] => ["Phone","VPN", "POP"] )

Desired result would be (based on array examples from above):

Array ( [0] => ["Tablet", "Budget","Phone", "VPN"] )

or

Array ( [0] => ["Tablet", "Budget", "Cloud", "Phone", "VPN"] )

or

Array ( [0] => ["Tablet", "Budget", "Cloud", "Phone", "VPN", "POP"] )

I tried with

call_user_func_array('array_merge', $myarray); 

and

$test = array_merge(...$myarray)

but i can't make it work, the values are not merged.

How can i do it?

PHP 7.3.1

Thank you

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Mircea T
  • 21
  • 2
  • 1
    `$test = [array_merge(...$myarray)];` fiddle: https://3v4l.org/LvDTn – u_mulder Apr 20 '20 at 11:54
  • Thank you, but my array is not like the one you've tested ($myarray = [["Tablet","Budget", "Cloud"],["Phone","VPN"]];) but is Array ( [0] => ["Tablet","Budget", "Cloud"] [1] => ["Phone","VPN"] ); do i have to do something before calling the array_merge in order to get an array like yours? – Mircea T Apr 20 '20 at 12:05
  • tested the code from fiddle and i'm getting the result Array ( [0] => ) – Mircea T Apr 20 '20 at 12:14
  • Your array is the same, `print-r` my array - there's no difference. – u_mulder Apr 20 '20 at 12:15
  • ok, but the result with your code is also different from what i've asked, i need a single key array, not a multikey array: Array ( [0] => ["Tablet", "Budget", "Cloud", "Phone", "VPN"] ) – Mircea T Apr 20 '20 at 12:22
  • Please, make sure you understand __arrays__. You have multidimensional array here. it has key `0` on level 1 and keys from 0 to 5 on level 2. – u_mulder Apr 20 '20 at 12:25
  • But i asked for a single level array, not a multidimensional array..can you help me, please? – Mircea T Apr 20 '20 at 12:34
  • `Array ( [0] => ["Tablet", "Budget", "Cloud", "Phone", "VPN", "POP"] )` is a __two level__ array. If you need one level - just use your code `$test = array_merge(...$myarray)`. – u_mulder Apr 20 '20 at 12:37

0 Answers0