8

COMPANY ARRAY

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 ARRAY

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

What I'd like the output to look like:

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); seems to not do what I want it do.

It adds it to the end, but not where i want it to (notice the difference in where it is placed):

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
} 
Matthew Johnson
  • 4,875
  • 2
  • 38
  • 51
ChicagoDude
  • 591
  • 7
  • 21

2 Answers2

4

It has another level inside $company, if you want the single array inside that another nesting, point it to index zero directly, and use array_merge:

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

Sample Output

Kevin
  • 41,694
  • 12
  • 53
  • 70
1

Another way to merge the two arrays is the + operator:

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

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

Community
  • 1
  • 1
Matthew Johnson
  • 4,875
  • 2
  • 38
  • 51