How to array merge in laravel

php array combine
laravel collection
php array merge unique
php merge multidimensional array
php merge array values with same keys
php merge array with same values
php combine array keys and values
php merge multidimensional array by key value

I have a problem here, I want to combine 2 arrays into 1, I've tried using array_merge from php, and merge () from laravel but nothing works //$plucked is EAV database

$vendor = Vendor::find($id)->toArray();
        $vendor_detail = Vendor_detail::where('vendor_id',$id)->get();
        $plucked = $vendor_detail->pluck('vendor_name','vendor_value');

        $merged = array_merge($plucked, $vendor);

        // $merged = $vendor->merge($plucked)->all(); 
        dd($merged);

I think because the array is different, there array $plucked

#items: array:10 [▼
    "user_email" => "cobaupdatelagi@gmail.com"

  ]

and there my array in $vendor

array:15 [▼
  "vendor_id" => 39
  "province" => "ACEH"

]

the output that I want

$somearray =[
   "vendor_id" => 39
  "province" => "ACEH"
  "user_email" => "cobaupdatelagi@gmail.com"

]

Your $vendor is one associative array while $plucked is an array of arrays. Even if it has only one item it will be of index zero so you need to loop through $plucked and merge for each one.

$vendor = Vendor::find($id)->toArray();
$vendor_detail = Vendor_detail::where('vendor_id',$id)->get();
$plucked = $vendor_detail->pluck('vendor_name','vendor_value');

$merged = [];
foreach($plucked as $p){
    $merged[] = array_merge($p, $vendor);
}
dd($merged);

PHP, The array_merge() is a builtin function in PHP and is used to merge two or more arrays into a single array. This function is used to merge the� merge() The merge method merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection:

$vendor = Vendor::find($id);
$vendor_detail = Vendor_detail::select('vendor_id','province')->where('vendor_id',$id)->get()->toArray();

$data= array_merge($vendor,$vendor_detail);

array_merge - Manual, Variable list of arrays to merge. Return Values �. Returns the resulting array. If called without any arguments, returns an empty array. Note: If two or more array elements have the same key, the last one overrides the others. Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See example below).

I think you should use database join to get faster results.

$vendor = Vendor::join('vendor_details', 'vendors.id', '=', 'vendor_details.vendor_id')
    ->select('vendors.*', 'vendor_name','vendor_value')
    ->where('id', $id)
    ->first();

if ($vendor)  {
    $vendor = $vendor->toArray();
}

Collections - Laravel, You can use the PHP array_merge() function to merge the elements or values of two or more arrays together into a single array. The merging is occurring in such � array_merge is a non-referential non-inplace right-reduction. For different ctrl-f typers, it's reduce-right, side-effect free, idempotent, and non in-place. ex:

  $vendor_detail = Vendor_detail::where('vendor_id',$id)->get(); will give you a collection

as well as $plucked = $vendor_detail->pluck('vendor_name','vendor_value'); will give you a collection, so, either you can use collection merge or you may like to loop through one and add merge another as other answer is doing, but these are not array, you are getting collections.

PHP array_merge() Function, Let us say we have an array request sent in from an HTML form to a Laravel controller. But then we need to add or merge values into that� The array_merge() is a builtin function in PHP and is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array.

How to Merge Two or More Arrays into One Array in PHP, Sep 13, 2017 � ErrorException array_merge(): Argument #1 is not an array when saving a model Laravel 5. this example will help you laravel collection merge� Today, We want to share with you Laravel Merge complex Eloquent queries.In this post we will show you Laravel merge array values with same keys, hear for Combine two DB query results in Laravel? we will give you demo and example for implement.In this post, we will learn about Merging two complex Eloquent queries with an example.

How to Merge New Values Into an Array Request on Laravel, How to combine and merge arrays in PHP using the array_combine and array_merge functions, and the array union operator. Helpers. Introduction; Available Methods; Introduction. Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.

Laravel array merge, Get code examples like "merge two array in laravel" instantly right from your google search results with the Grepper Chrome Extension. All do not work for me on eloquent collections, laravel eloquent collections use the key from the items I think which causes merging issues, you need to get the first collection back as an array, put that into a fresh collection and then push the others into the new collection;

Comments
  • what you want in here??expected result here?
  • @albus_severus there's my expected result $somearray =[ "vendor_id" => 39 "province" => "ACEH" "user_email" => "cobaupdatelagi@gmail.com" ]