1

I need my processing function to receive the entire original array minus 1 value which i am deleting in a loop. However the next call needs to have the original value added back and the next value removed.

The array keys are non numeric so I cannot use a for loop, because I need the key value. Below is the array i am sending to processing function().

$stuff = array(
        'inner'=>array('A=>Alpha','B=>Beta','C=>Charlie'),
        'outer'=>array('C=>Cat','D=>Dock','K=>Kite')
              );      

This is what the processing function should receive during each loop iteration.
Loop 1:  
     array(
    'inner'=>array('B=>Beta','C=>Charlie'),
    'outer'=>array('C=>Cat','D=>Dock','K=>Kite')
          )
Loop 2:  
     array(
    'inner'=>array('A=>Alpha','C=>Charlie'),
    'outer'=>array('C=>Cat','D=>Dock','K=>Kite')
          )
Loop 3: 
     array(
    'inner'=>array('A=>Alpha','B=>Beta'),
    'outer'=>array('C=>Cat','D=>Dock','K=>Kite')
          )

CODE:

Causes an infinite Loop because each value is immediately added onto the end.

 foreach($stuff as $term=>&$arr)
   {
       switch($term)
       {
         case 'inner':
          foreach($arr as $key=>$value)
           {
               //remove value
               unset($arr[$key]);
               processing($stuff);
               //add it back
               $arr[$key] = $value;
           }
         break;
       }
   }

Causes it to run an extra time:

foreach($stuff as $term=>&$arr)
       {
           switch($term)
           {
             case 'inner':
              $last_key = end(array_keys($arr));
              foreach($arr as $key=>$value)
               {
                    unset($arr[$key]);
                    processing($stuff);
                    //put it BACK
                    if($key != $last_key)
                    {
                        $arr[$key] = $value;
                    }
               }
             break;
           }
       }

What is the best approach to solving this problem?

4
  • Why not processing($stuff, $key_to_be_ignored)? Then you don't have to use references in your foreach. Otherwise, you're going to get that behavior because that's how arrays work in PHP. Commented Jan 14, 2016 at 23:23
  • @jbafford unfortunately i cannot modify the processing function or i would of done that approach. Commented Jan 14, 2016 at 23:24
  • In your example iterations only $stuff['inner'] is modified, that intended or is there a loop 4, loop 5 ....? Commented Jan 14, 2016 at 23:26
  • @VolkerK that is handled in a different case statement where stuff['inner'] is held constant while $stuff['outer'] is modified. Commented Jan 14, 2016 at 23:30

1 Answer 1

1

You can iterate through the inner items and make a new copy of the array each time, then unset the current key. With this approach you don't have to worry about adding/removing items from the original array while iterating over it.

$items = ['A', 'B', 'C'];

foreach ($items as $key => $item) {
    $otherItems = $items;
    unset($otherItems[$key]);

    print_r($otherItems);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Since it's so much easier than my FilterIterator, I'd rather use this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.