string(4) "3626" ["entityparentid"]=> string(1) "0" ["entitydupl...">

PHP array not working

COMPANY MASSES

array(1) { [0]=> array(19) { ["entityid"]=> string(4) "3626" ["entityparentid"]=> string(1) "0" ["entityduplicateof"]=> string(1) "0" ["entitytype"]=> string(1) "0" ["entityname"]=> string(12) "Facebook Inc" } } 

DISTANCE OF DISTANCE

 array(1) { ["distance"]=> string(4) "1.22" } 

What I would like the result to look like this:

 array(1) { [0]=> array(19) { ["entityid"]=> string(4) "3626" ["entityparentid"]=> string(1) "0" ["entityduplicateof"]=> string(1) "0" ["entitytype"]=> string(1) "0" ["entityname"]=> string(12) "Facebook Inc" ["distance"]=> string(4) "1.22" // here } } 

Question:

array_push($company_array,$distance_array); doesn't seem to do what I want.

He adds it to the end, but not where I want (note the difference in where it is):

 array(1) { [0]=> array(19) { ["entityid"]=> string(4) "3626" ["entityparentid"]=> string(1) "0" ["entityduplicateof"]=> string(1) "0" ["entitytype"]=> string(1) "0" ["entityname"]=> string(12) "Facebook Inc" }, ["distance"]=> string(4) "1.22" // not here } 
+8
arrays php
source share
2 answers

It has a different level inside $company if you want one array inside this other nesting to point directly to index zero and use array_merge :

 $company[0] = array_merge($company[0], $distance); 

Output example

+4
source share

Another way to merge two arrays is the + operator:

 $company[0] = $company[0] + $distance; 

A detailed explanation of the difference between array_merge and + can be found here.

+1
source share

All Articles