0

i have 2 arrays with different dimensions, and how to merge my one dimension array to two dimension array, here is my 2 array look like

this is my two dimension array

array (
0 => 
array (
'id' => 1,
'alias' => 'Anderson',
'location' => 'Semarang',
    'up' => 39144,
'status' => 'DOWN',
),

and this is my one dimension array

 array (
  'last_accessed_by' => 1,
  'last_refresh' => '2018-10-29 00:21:39',
   'created_at' => '2018-10-29 00:21:39',
 )

this is what i expect from merging the arrays

array (
  0 => 
  array (
    'id' => 1,
    'alias' => 'Anderson',
    'location' => 'Semarang',
    'up' => 39144,
    'status' => 'DOWN',
    'last_accessed_by' => 1,
    'last_refresh' => '2018-10-29 00:21:39',
    'created_at' => '2018-10-29 00:21:39',
  ),
5
  • im using laravel so thats the dd output from, Commented Oct 29, 2018 at 0:01
  • $data=json_decode($response,true); this is the array coming from, i'm not input it, just decode from response Commented Oct 29, 2018 at 0:16
  • i just edited my posts Commented Oct 29, 2018 at 0:35
  • Please make every attempt to progress all of your questions toward a system recognized resolution (simply put, when you receive at least one answer that solves your issue, please award the green tick to the best answer). Commented Oct 29, 2018 at 1:40
  • thanks anyway for correcting my posts Commented Oct 29, 2018 at 1:52

1 Answer 1

1

You need to merge the [0] element of the first array and the whole second array like this...

Code: (Demo)

$array1 = array (
    array (
        'id' => 1,
        'alias' => 'Anderson',
        'location' => 'Semarang',
        'up' => 39144,
        'status' => 'DOWN'
    )
);

$array2 =  array (
    'last_accessed_by' => 1,
    'last_refresh' => '2018-10-29 00:21:39',
    'created_at' => '2018-10-29 00:21:39'
);
$array1[0] = array_merge($array1[0], $array2);
// or because merging associative arrays, if you aren't scared of array union operators:
// $array1[0] += $array2;
//            ^^-- this merges the 2nd to [0] of the 1st without a function call

var_export($array1);

Output:

array (
  0 => 
  array (
    'id' => 1,
    'alias' => 'Anderson',
    'location' => 'Semarang',
    'up' => 39144,
    'status' => 'DOWN',
    'last_accessed_by' => 1,
    'last_refresh' => '2018-10-29 00:21:39',
    'created_at' => '2018-10-29 00:21:39',
  ),
)
Sign up to request clarification or add additional context in comments.

2 Comments

this is exactly what i looking for, thanks a lot for help,
I would like to upvote your question, but my StackOverflow Good Citizenship principles disallow it while your question does not contain a coding attempt. If you add this, please let me know and I'll come back.

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.