简体   繁体   English

如何在PHP中将某个键的值从两个数组添加到一个新数组中?

[英]How do i add values of a certain key from two arrays into one new array in PHP?

I need to amalgamate some notices into an overall array. 我需要将一些通知合并为一个整体。

   array today = array( 
'keya' => '11',
'keyb' => 'string',
'notice' => 'Visitor Arrives at 3pm',
'keyd' => '44',
...
);

array general = array( 
'keya' => '1',
'notice' => 'Fire Alarms Tested This Week'
);

I want to have a new array called $general_notice which merges the $today['notice'] with $general['notice']. 我想要一个名为$ general_notice的新数组,该数组将$ today ['notice']与$ general ['notice']合并。

I can't understand what to do using operators or array_merge as i only one to merge the data of a particular key... 我不明白使用运算符或array_merge做什么,因为我只有一个来合并特定键的数据...

NB $general may have multiple rows ie multiple notices... 注意:$ general可能有多行,即多条通知...

I think i cant use array_merge because the arrays are multi-dimensional. 我想我不能使用array_merge因为数组是多维的。

EG: 例如:

//1st array with lots of data but the data belonging to the 'notice' key is what i'm interested in.
array $today = [0]('id'=>'1','lotsOfKeys'=>"lots of values,'notice'="Visitor arrives at 3pm");

array $general = [0]('id'=> '1', 'notice' = "Fire Alarms Tested This Week"),
                 [1]('id'=> '2', 'notice' = "Blue Shift working");

The desired result is: 理想的结果是:

$notices = ("Visitor arrives at 3pm", "Fire Alarms Tested This Week", "Blue Shift Working");

Since you are using arrays with keys and each array can't have duplicate keys, your desired result would be as simple as: 由于您使用带有键的数组,并且每个数组不能有重复的键,因此所需的结果将很简单:

array general_notice = array($today['notice'], $general['notice']);

unless you have a two dimensional array of arrays, for example: 除非您具有二维数组数组,例如:

array general = array(
     array( 
         'keya' => '11',
         'keyb' => 'string',
         'notice' => 'Visitor Arrives at 3pm',
         'keyd' => '44'
      ),
      array( 
          'keya' => '1',
          'notice' => 'Fire Alarms Tested This Week'
      ),
      ...
);

In that case, you would need to loop though the notices array and push desired values to a new one like so. 在这种情况下,您将需要遍历notices数组,然后将所需的值推入一个新值。 You can repeat the loop for each array you want to append to the general_notice array: 您可以为要附加到general_notice数组的每个数组重复循环:

array general_notice = array();
foreach ($general as $value) {
    array_push($general_notice, $value['notice'] );
}

You can also append any single dimensional arrays as follows 您还可以按如下所示附加任何一维数组

array_push($general_notice, $today['notice']);

The question is unclear, what do you mean by merging them? 问题尚不清楚,将它们合并意味着什么? Are you trying to concatenate the values of the keys based on both arrays having the same key? 您是否尝试根据具有相同键的两个数组来串联键的值?

暂无
暂无

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

相关问题 如何组合两个 arrays,一个作为键,另一个作为数组? - How do I combine two arrays, one as a key and the other as an array? Laravel PHP如何遍历数组数组并添加新的键值 - Laravel php How to loop through array of arrays and add new key values 如何根据键值对将两个数组合并为一个数组 - how to merge two arrays in one array based on key and values pair 如何从PHP中的键=>“值的数组”数组变为“键=>值”的数组数组 - How do I go from an array of key => “array of values” to an array of an array of “key => values” in php 如果它符合php中的某些条件,如何在多维数组中的每个数组中添加键? - How do I add a key to each array in a multidimensional array if it meets certain criteria in php? 如何在将它们组合到 php 的同时创建一个新数组,其中仅包含来自 2 个单独的 arrays 的重复项的一个实例? - How do I make a new array containing just one instance of duplicates from 2 separate arrays while combining them in php? 如何合并两个关联数组,以使键冲突导致两个值组成一个数组? - How do I merge two associative arrays such that key collisions result in an array of both values? 如何在一个数组php中合并两个数组值 - how to merge two arrays values in one array php 如何在多维数组PHP中添加新键并合并数组值 - How to Add a new Key and Merge the array values in multidimemsional array PHP PHP 将两 (2) 个值(非键)从一个数组复制到新数组 - PHP Copy two (2) Values (Not Keys) from one array into new array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM