-1

I have 2 arrays:

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

$otherarray["otherkey"];

I want to merge it and that $otherarray["otherkey"] is right after $array["b"]

I am using array_merge($array,$otherarray) but this creates this array:

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

Is there a way to insert it right after ["b"] so it becomes:

$newarray["a"];
$newarray["b"];
$newarray["otherkey"];
$newarray["c"];
$newarray["d"];
prgrm
  • 3,734
  • 14
  • 40
  • 80
  • A simple foreach doesn't do the job? Did the `otherkey` can change or it will be always the same? Do you need to add this `otherkey` after `b` all the time or it can change too? – Mickaël Leger Jun 22 '18 at 07:54
  • @MickaelLeger I simplified it a lot, these are multidimensional arrays that come from functions I have no control over and cannot change, so if there is a PHP function that already does this it would be great, instead of bloating the code even more. – prgrm Jun 22 '18 at 07:55
  • Do you know where is your specific key, in this case, key `b` ? Do you have posibilty to identify it? – GasKa Jun 22 '18 at 07:55
  • @GasKa yes, b is always in the same place. – prgrm Jun 22 '18 at 07:56
  • 1
    See also for a slice and glue: https://stackoverflow.com/q/1783089/3392762 – Progrock Jun 22 '18 at 08:44

2 Answers2

2

Maybe you can try somehting like this :

// 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);

Output is :

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)
Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
2

This is another option:

<?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"));
GuyT
  • 21
  • 3