【问题标题】:Merge 2 arrays after a specific key in PHP在 PHP 中的特定键之后合并 2 个数组
【发布时间】:2018-06-22 07:52:54
【问题描述】:

我有 2 个数组:

$array["a"];
$array["b"];
$array["c"];
$array["d"];

$otherarray["otherkey"];

我想合并它,$otherarray["otherkey"] 就在$array["b"] 之后

我正在使用array_merge($array,$otherarray),但这会创建这个数组:

$newarray["a"];
$newarray["b"];
$newarray["c"];
$newarray["d"];
$newarray["otherkey"];

有没有办法在 ["b"] 之后插入它,所以它变成:

$newarray["a"];
$newarray["b"];
$newarray["otherkey"];
$newarray["c"];
$newarray["d"];

【问题讨论】:

  • 一个简单的 foreach 不能完成这项工作? otherkey 是否可以更改或将始终相同?您是否需要一直在b 之后添加此otherkey,或者它也可以更改?
  • @MickaelLeger 我简化了很多,这些是来自我无法控制且无法更改的函数的多维数组,所以如果有一个 PHP 函数已经这样做了,那就太好了,而不是让代码更加臃肿。
  • 您知道您的特定密钥在哪里吗,在这种情况下,密钥b ?你有可能识别它吗?
  • @GasKa 是的,b 总是在同一个地方。
  • 切片和胶水请参见:stackoverflow.com/q/1783089/3392762

标签: php


【解决方案1】:

也许你可以试试这样:

// Your first array
$array["a"] = "a";
$array["b"] = "b";
$array["c"] = "c";
$array["d"] = "d";

// Your second array
$otherarray["otherkey"] = "otherkey";

// I create a function with 4 param :
// $first_array : the first array you want to merge
// $second_array : the second array you want to merge
// $merge_after_key : the key of the first array you will use to know when your merge the second array
// $otherkey : the key of the second array you will merge
function mergeArrayByKey($first_array, $second_array, $merge_after_key, $otherkey) {
    // I create a new array
    $new_array = array();

    // Now I will loop through the first array and create the new array for each key
    foreach ($first_array as $key => $value) {
        $new_array[$key] = $value;

        // When key of first array meet the merge after key : I know I need to add the second array to my result array
        if ($key === $merge_after_key) {
            $new_array[$otherkey] = $second_array[$otherkey];
        }
    }
    // Then I return the new array
    return $new_array;
}

// Now you can use it and change the value if you need.
// For your example I use your two array + the key 'b' and the 'otherkey'
$result = mergeArrayByKey($array, $otherarray, 'b', 'otherkey');

var_dump($result);

输出是:

array (size=5)
  'a' => string 'a' (length=1)
  'b' => string 'b' (length=1)
  'otherkey' => string 'otherkey' (length=8)
  'c' => string 'c' (length=1)
  'd' => string 'd' (length=1)

【讨论】:

    【解决方案2】:

    这是另一种选择:

    <?php
    $first = ["a" => 1, "b" => 2, "c" => 3, "d" => 4];
    $second = ["inserted" => "here", "and" => "also here"];
    
    function mergeAtKey($first, $second, $key) {
        $bothParametersAreArrays = is_array($first) && is_array($second);
        $requestedKeyExistsInFirstArray = array_key_exists($key, $first);
        if (!($bothParametersAreArrays && $requestedKeyExistsInFirstArray)) {
            throw new Exception;
        }
        $keyPositionInFirstArray = array_search($key, array_keys($first)) + 1;
        $firstSlice = array_slice($first, 0, $keyPositionInFirstArray, true);
        $secondSlice = array_slice($first, $keyPositionInFirstArray, count($first) - 1, true);
        return $firstSlice + $second + $secondSlice;
    }
    
    print_r(mergeAtKey($first, $second, "b"));
    

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多