简体   繁体   English

PHP数组合并不起作用

[英]PHP Array combining not working

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
} 

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内部有另一个级别,如果您希望单个嵌套的数组位于另一个嵌套中,则将其直接指向索引为零,然后使用array_merge

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

Sample Output 样本输出

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 . 关于array_merge+的区别的详细说明可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM