3

I have an array where all arrays where key type == 'foo' must be replaced by a custom arrays. Basically I need to find a specific array and then replace it with other arrays.

The issue here you can easily replace one array but when you insert numbers of arrays you are shifting keys so the next array type == 'foo' will not be replaced

Any help would be appreciated.

Here's what I have:

$array = array(
   array(
     'options' => array(
        array(
          'type' => 'foo'
        ),
        array(
          'type' => 'foo'
        ),
        array(
          'type' => 'bar'
        )
      )
   ),
   array(
     'options' => array(
        array(
          'type' => 'bar'
        ),
        array(
          'type' => 'bar'
        ),
        array(
          'type' => 'foo'
        )
      )
   ),
);

And I have an array which should replace any array where type == 'foo'

$array_foo = array(
  array(
    'type' => 'custom'
  ),
  array(
    'type' => 'custom_2'
  ),
  array(
    'type' => 'anything'
  ),
);

Here is the desired output:

$array = array(
   array(
     'options' => array(
        array(
          'type' => 'custom'
        ),
        array(
          'type' => 'custom_2'
        ),
        array(
          'type' => 'anything'
        ),
        array(
          'type' => 'custom'
        ),
        array(
          'type' => 'custom_2'
        ),
        array(
          'type' => 'anything'
        ),
        array(
          'type' => 'bar'
        )
      )
   ),
   array(
     'options' => array(
        array(
          'type' => 'bar'
        ),
        array(
          'type' => 'bar'
        ),
        array(
          'type' => 'custom'
        ),
        array(
          'type' => 'custom_2'
        ),
        array(
          'type' => 'anything'
        ),
      )
   ),
);

Thank you.

Goran Kutlaca
  • 2,014
  • 1
  • 12
  • 18
Artem
  • 755
  • 5
  • 19
  • What have you tried so far ? – AymDev Aug 31 '18 at 14:18
  • @AymDev simple foreach find array then unset it then array_merge with array_slice to insert new data instead of unsettled one but as i said it works only for first founded array – Artem Aug 31 '18 at 14:24
  • Glad you tried ! Edit your question and post your attempt so we can see what's wrong and help you about that. – AymDev Aug 31 '18 at 14:26

2 Answers2

1

Here's a way using 2 nested foreach loops and array_merge() with a temporary array:

// Pass the array by reference
foreach ($array as &$sub) {
    // Temporary array
    $new_options = [];
    // Loop through options
    foreach ($sub['options'] as $opt) {

        // if type foo: replace by $array_foo items
        if ($opt['type'] == 'foo') {
            $new_options = array_merge($new_options, $array_foo);

            // else, keep original item
        } else {
            $new_options[] = $opt;
        }
    }

    // replace the options
    $sub['options'] = $new_options;
}

And check the output:

echo '<pre>' . print_r($array, true) . '</pre>';

See also Passing by Reference

AymDev
  • 6,626
  • 4
  • 29
  • 52
-2

According to a previous post, the code for changing could be done in a similar fashion to the following code:

$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");

$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);

So with your method instead of having numbers or indexes, I think you might be able to get away with writing down foo instead to get the replacement to work properly.

The other thing you might look into is array merge-recursive which can be found at: http://php.net/array_merge_recursive

The problem you stated here looks similar to this post: PHP Array Merge two Arrays on same key

I hope this helps you.

seansanders
  • 95
  • 10