Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two arrays:

Array
(
    [0] => 5
    [1] => 4
)
Array
(
    [0] => BMW
    [1] => Ferrari
)

And I would like to have that result. Merge the values with the same key

Array  
  (  
    [0] => Array  
      (  
        [0] => 5  
        [1] => BMW 
      )  
    [1] => Array  
      (  
        [0] => 4
        [1] => Ferrari 
      )  
  )  

How could I do that? Is there any native PHP function that does this? I tried array_merge_recursive and array_merge but did not get the expected result

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
335 views
Welcome To Ask or Share your Answers For Others

1 Answer

As to @splash58 comment:

You can use array-map. Here an example:

$array = [["5", "4"], ["BMW", "Ferrari"]];
$res = array_map(null, ...$array);

Now res will contain:

Array
(
    [0] => Array
        (
            [0] => 5
            [1] => BMW
        )
    [1] => Array
        (
            [0] => 4
            [1] => Ferrari
        )
)

If the array in 2 different var you can use:

$res= array_map(null, ["5", "4"], ["BMW", "Ferrari"]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...