-1

I am trying to create new array by checking index number and fill that index by another array.

Here in loop if current element index is in index 4

it will take value from another array.

and the value on that position will move to next loop.

My current two array like this

$arr1 = array(
    0 => 'number 1', 
    1 => 'number 2',
    2 => 'number 3',
    3 => 'number 4',
    4 => 'number 5',
) ;


$arr2 = array(
    0 => 'number 4 insert', 
    1 => 'number 8 insert',
    2 => 'number 12 insert',
    3 => 'number 16 insert',
    4 => 'number 24 insert',
) ;

My expected new array I want like this

Array
(
    [0] => 'number 1',
    [1] => 'number 2',
    [2] => 'number 3',
    [3] => 'number 4 insert',
    [4] => 'number 4',
    [5] => 'number 5',
    [6] => 'number 6',
    [7] => 'number 8 insert',
    [8] => 'number 7',
    [9] => 'number 8',
    [10] => 'number 9',
    [11] => 'number 12 insert',
    [12] => 'number 10',
    ...
    ...
)

For this solution I am trying this but it generally replacing my main array value not by pushing value on selected key index I want.

$arr1 = array(
    0 => 'number 1', 
    1 => 'number 2',
    2 => 'number 3',
    3 => 'number 4',
    4 => 'number 5',
) ;


$arr2 = array(
    0 => 'number 4 insert', 
    1 => 'number 8 insert',
    2 => 'number 12 insert',
    3 => 'number 16 insert',
    4 => 'number 24 insert',
) ;


function match_arrayKeys ($x, $y,$z)
{
    $keys    = array_keys ($x);
    
    $keysy    = array_keys ($y);

    for ($i = 0; $i < count ($keys); $i++)
    {
        $j = $i+1;
        if($j%$z== 0 ){
            $newarray [$i] = $y[$i-1];
        } else {
          $newarray [$i] = $x[$i];
        }
        
    }
    return $newarray;
}

print_r (match_arrayKeys ($arr1,$arr2, 4));

FIDDLE

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
  • What is the logic behind this desired order? And why do your strings say "replaced" when they need to be inserted? – El_Vanja Dec 01 '20 at 15:29
  • Does this answer your question? [How to insert element into arrays at specific position?](https://stackoverflow.com/questions/3353745/how-to-insert-element-into-arrays-at-specific-position) – El_Vanja Dec 01 '20 at 15:29
  • @El_Vanja ok I changed my string text. – Lemon Kazi Dec 01 '20 at 15:34
  • Where is `$arr2` coming from? Might it make this task easier to update `$arr2`'s keys to be the position in the resulting array where you want want each element? Such as `$arr2 = [3 => 'number 4 insert', 7 => 'number 8 insert']`? – gen_Eric Dec 01 '20 at 15:39
  • Let me see if I understand your question: you have an array which has input in the format of "number i" where i is the index of the element and a number z which has an integer. Now on passing to the function, you want every zth element to say "number z replaced"? Is it so? – freeroamer90 Dec 01 '20 at 15:41
  • How are you expecting to generate the occurances that dont exist in either array? You base your loop on the size of `$keys` so the result will never be what you want – RiggsFolly Dec 01 '20 at 15:41
  • @RiggsFolly yes i know that loop will be not much. So i stacked how can I create my expected array. – Lemon Kazi Dec 01 '20 at 15:43
  • @freeroamer90 no 'number i' is not my case. i want key inded will check and value from another array will push on that index. old index will move to next index – Lemon Kazi Dec 01 '20 at 15:45
  • I dont know what the magic word is that you want us to use to magic up `'number 6', 'number 7', `'number 8'` .... etc etc That dont exist in either array – RiggsFolly Dec 01 '20 at 15:45
  • @LemonKazi so what is the value z? and will the 2nd array be always in the format of "number i replaced"? – freeroamer90 Dec 01 '20 at 15:46
  • @freeroamer90 value z is just the key position like number 4 if (index $i+1) % 4 ==0 means it will check every 4,8,12 ... key position – Lemon Kazi Dec 01 '20 at 15:48

1 Answers1

1

You can try something like this:

function match_arrayKeys($arr1, $arr2, $gap)
{
    $index = $gap - 1;
    while ($element = array_shift($arr2)) {
        array_splice($arr1, $index, 0, $element);
        $index += $gap;
        if ($index > count($arr1)) break;
    }
    return $arr1;
}

print_r(match_arrayKeys($arr1, $arr2, 4));
id'7238
  • 2,428
  • 1
  • 3
  • 11
  • thanks for your answer. it close.. I want all value from two arrays. also just according to position 2nd array value will display. – Lemon Kazi Dec 01 '20 at 15:58
  • 1
    Just remove the break condition. Then the rest of the second array will be at the end. You described the expected result is not entirely clear for the original arrays. Its dimension exceeds both of the original ones. – id'7238 Dec 01 '20 at 16:17