在使用 array_merge 之前扩展 PHP 数组一层。

huangapple go评论70阅读模式
英文:

Expand PHP Array one level before array_merge

问题

我有一个像这样的简单数组:

Array(
  [Key1] => value1
  [Key2] => value2
  [Key3] => value3
)

我使用类似的数组合并函数(array_merge)与另一个类似的数组合并,得到了这个结果。
(如果键已经存在,只会更新值)

Array(
  [Key1] => value1
  [Key2] => value2
  [Key3] => value3
  [Key4] => value4
  [Key5] => value5
)

但我想要在每个项目添加或更新时添加一个日期。像这样:

Array(
   [0] => Array(
      [Key1] => Value1
      [Date] => 2023-07-10 02:14
   )
   [1] => Array(
      [Key2] => Value2
      [Date] => 2023-06-25 05:30
   )
   [2] => Array(
      [Key3] => Value3
      [Date] => 2023-07-12 16:40
   )
   [3] => Array(
      [Key4] => Value4
      [Date] => 2023-07-13 09:51
   )
   [4] => Array(
      [Key5] => Value5
      [Date] => 2023-07-13 09:51
   )
)

所以新的数据一开始是一个一级数组,我需要为每个键/级别添加当前日期/时间(格式目前不重要),然后将其与旧数组合并。旧数组将始终存储为一个二级数组。

希望我表达清楚了 在使用 array_merge 之前扩展 PHP 数组一层。

英文:

I have a simple array like this:

    Array(
      [Key1] => value1
      [Key2] => value2
      [Key3] => value3
    )

which I merge with a similar array (array_merge) and gets this.
(If the key already exist only the value will be updated)

    Array(
      [Key1] => value1
      [Key2] => value2
      [Key3] => value3
      [Key4] => value4
      [Key5] => value5
    )

But I want to add a date to when each item was either added or updated. Like this:

    Array(
       [0] => Array(
          [Key1] => Value1
          [Date] => 2023-07-10 02:14
       )
       [1] => Array(
          [Key2] => Value2
          [Date] => 2023-06-25 05:30
       )
       [2] => Array(
          [Key3] => Value3
          [Date] => 2023-07-12 16:40
       )
       [3] => Array(
          [Key4] => Value4
          [Date] => 2023-07-13 09:51
       )
       [4] => Array(
          [Key5] => Value5
          [Date] => 2023-07-13 09:51
       )
)

So the new data is a one level array at first, I need to add a the current date/time (Format not important right now) to each key/level and then merge it with the old array. The old array will always be stored as a two level array.

I hope I make any sense here 在使用 array_merge 之前扩展 PHP 数组一层。

答案1

得分: 3

你可以通过分别循环遍历两个数组来实现这个操作。这应该可以在总共有多少个数组都适用。

$arrayOne = [
    'Key1' => 'value1',
    'Key2' => 'value2'
];

$arrayTwo = [
    'Key2' => 'value2'
];

foreach([$arrayOne, $arrayTwo] as $array)
{
    foreach($array as $key => $value)
    {
        $output[$key] = [
            $key => $value,
            'Date' => date('Y-m-d H:i:s.u')
        ];
    }
    
    sleep(1); // 仅用于测试目的。
}

由于这几乎瞬间运行,我不得不添加 sleep(1) 以显示它的工作情况。第二个值是唯一被覆盖的值,因此显示了更新的时间戳。

array(2) {
    ["Key1"] => array(2) {
        ["Key1"] => string(6) "value1"
        ["Date"] => string(26) "2023-07-13 08:08:49.000000"
    }
    ["Key2"] => array(2) {
        ["Key2"] => string(6) "value2"
        ["Date"] => string(26) "2023-07-13 08:08:50.000000"
    }
}
英文:

You can do this by looping over both arrays individually. This should work regardless of how many arrays you have in total.

$arrayOne = [
	'Key1' => 'value1',
	'Key2' => 'value2'
];

$arrayTwo = [
	'Key2' => 'value2'
];

foreach([$arrayOne, $arrayTwo] as $array)
{
	foreach($array as $key => $value)
	{
		$output[$key] = [
			$key => $value,
			'Date' => date('Y-m-d H:i:s.u')
		];
	}
	
	sleep(1); // Just for testing purposes.
}

Since this runs almost instantly, I had to add sleep(1) to show that it works. The second value is the only value that is overwritten and thus shows the updated timestamp.

array(2) {
    ["Key1"] => array(2) {
        ["Key1"] => string(6) "value1"
        ["Date"] => string(26) "2023-07-13 08:08:49.000000"
    }
    ["Key2"]=> array(2) {
        ["Key2"] => string(6) "value2"
        ["Date"]=> string(26) "2023-07-13 08:08:50.000000"
    }
}

huangapple
  • 本文由 发表于 2023年7月13日 16:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定