0

I have two array from two query. I want to join two array into one array. Example:

Array1

[
   {
       fakultas: "Ekonomi dan Bisnis"
   },
   {
       fakultas: "Hukum"
   }
]


Array2

[
   {
       jumlah_wisudawan: "55"
   },
   {
       jumlah_wisudawan: "17"
   }
]

How to join two array above into one array like this:

[
   {
       fakultas: "Ekonomi dan Bisnis",
       jumlah_wisudawan: "55"
   },
   {
       fakultas: "Hukum",
       jumlah_wisudawan: "17"
   }
]

Please help me, thanks before.

1
  • What's the actual structure of your data? The code supplied looks like JavaScript, not PHP. So are the nested elements stdClass instances, or arrays? Commented Apr 11, 2017 at 1:28

2 Answers 2

1

You could use collections:

https://laravel.com/docs/5.4/collections

$collection = collect($array1, $array2);

and if you want to get that array, you can do it like this:

$collection->all();
Sign up to request clarification or add additional context in comments.

Comments

1

You should use array_merge not array_combine

array_merge($array1, $array2);

See documentation of PHP array_combine vs. array_merge

3 Comments

True don't use array_combine I messed up on my answer!! But not sure that array_merge is the result he really wants :/
he can use also the function of Laravel collect()
Or doing it with a nice old foreach loop :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.