简体   繁体   English

将数据推送或合并到现有阵列中

[英]Push or Merge data into existing Array

When working with existing code, it takes one array and places it into another in the fashion shown below. 使用现有代码时,它需要一个数组并将其按以下所示方式放入另一个数组。

I believe the empty brackets are the same thing as simply pushing it and appending it to the first available index. 我相信空括号与简单地推入并将其附加到第一个可用索引相同。

$g['DATA'][] = $p;

After this is done, I have my own array that I would like to append to this as well. 完成此操作后,我将有一个自己的数组,也想附加到此数组上。 I tried using array_merge() with $g['DATA'][] as a parameter, but this is invalid for obvious reasons. 我尝试将array_merge()$g['DATA'][]作为参数一起使用,但这出于明显的原因是无效的。

My only thought is to create a foreach loop counter so I can figure out the actual index it created, however I have to assume there is some cleaner way to do this? 我唯一的想法是创建一个foreach循环计数器,以便我可以弄清楚它创建的实际索引,但是我必须假设有一些更简洁的方法可以做到这一点?

Just simply use the count() of your $g["DATA"] array as index and then you can merge it like this: 只需将$g["DATA"]数组的count()用作索引,然后就可以像这样合并它:

$g['DATA'][count($g["DATA"])-1] = array_merge($g['DATA'][count($g["DATA"])-1], $ownArray);
         //^^^^^^^^^^^^^^^^^^^                           ^^^^^^^^^^^^^^^^^^^
         // -1 Because an array is based index 0 -> means count() - 1 = last key 

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

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