【问题标题】:PHP associative array formation [duplicate]PHP关联数组形成[重复]
【发布时间】:2018-08-11 12:16:20
【问题描述】:

我有两个类似的数组:

$result = ["should" => 1];
$result1 = ["must" => 4, "should" => 3];

我需要这样的单个数组的结果:

["should" => 1, "must" => 4, "should" => 3]

我得到了这个结果:

["should" => [1,3], "must" => 4]

【问题讨论】:

  • 不可能有两次相同的密钥。

标签: php arrays multidimensional-array associative-array


【解决方案1】:

您可以使用array_merge_recursive,因为key 是重复的,因此您无法获得与您提到的具有相同键should 的关联数组。

$result = ["should" => 1];
$result1 = ["must" => 4, "should" => 3];
print_r(array_merge($result,$result1));

使用array_merge_recursive 会输出你

["should" => [1,3], "must" => 4] 是有效的关联数组。

【讨论】:

    猜你喜欢
    • 2017-10-26
    • 2023-03-15
    • 1970-01-01
    • 2019-09-11
    • 2015-11-09
    • 1970-01-01
    • 2021-12-13
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多