tags:

views:

1151

answers:

6
foreach($arrayOne as $value){
    do function
}

In the above example, I'd like to pass $arrayOne into a loop, have a function operate that removes some elements of $arrayOne and then have the loop pass over the reduced $arrayOne on the elements that are left until the loop returns false.

Recommendations?

+1  A: 

i would recommend having two arrays,
one with the data - dataarray,
the other initially empty - emptyarray
and whatever qualifies in the first foreachloop, u push into the 2nd array, and at the end of that, empty the first array, swap the identifiers of the two arrays (dataarray becomes emptyarray and viceversa) and repeat until you return false or whatever

adi92
+5  A: 

Do you just need a function to remove some elements of an array?

If so, you can use array_filter.

Dinoboff
+1  A: 

If you want to make modifications to the value of array items, use by reference. If you want to entirely remove array items, split out the key / value pairs.

$arrayOne = array('example', 'listing of', 'stuff');

foreach ($arrayOne as $key => &$value) {
    $value .= ' alteration';

    if ($value == 'listing of alteration') {
        unset($arrayOne[ $key ]);
    }

}

The above code will add the text " alteration" to the end of each item in the array. It will also remove the second index when it matches "listing of alteration". Tested on PHP 5.2

Will unset() remove the array element from $arrayOne so that it is missing as the foreach() loop steps to the next array element?
kevtrout
A: 

I think a while loop would be a better bet in this instance.

KernelM
+1  A: 

Rabbit has the right answer for using references to edit values and index to unset in a foreach loop (I would vote you up but this is my first post so I don't have 15 rep yet, sorry)

be sure to remember to use the reference if you pass it through to a function that needs to edit the value as well. You would also need to pass the array as a reference if it's to remove the value from it.

I'd recommend making the function return boolean on whether to remove to prevent creating more references. e.g.

foreach ($array AS $key => &$value) {
  //& reference only needed if execFunction must edit $value
  if (execFunction(&$value)) { 
    unset($array[$key]);
  } else {
    $value['exec_failed']+=1;
  }
}
unset($value);

also the $value reference will persist past the scope of the loop, thus the trailing unset.

A last thought, it sounded as if you wanted to loop through the array multiple times. Be sure to pay attention to how your loop stops executing.

The loop stops when the last item in the array is processed or removed
kevtrout
"Fatal error: Cannot use assign-op operators with overloaded objects nor string offsets" You have to give $value['exec_failed'] a value before you can increment it.
gradbot
A: 

Thanks everyone for their help. The unset() function is, in the end, what is useful to me in today's specific need. Here's the short story of my use of unset():

//array to be processed
$one=array('1','2','3');
//array of terms previously defined as a group to compare against
$against=array('1','2');

foreach($one as $key=>$value){

    //pull out matching terms   
    $match=array_intersect($one,$against);
    //my need is to sum grouped numbers
    $sum=array_sum($match);
    echo $sum."<br />";

    //remove matching terms from original array
    foreach($match as $key=>$value){
     unset($one[$key]);
    }
}
//now a parent looping function will process the remaining array elements. In this case, the only one left is '3'.
kevtrout
You don't need the first loop:$match=array_intersect($one,$against);foreach($one as $key=>$value){ if (in_array($Value, $match)) { unset($one[$key]); }}
Dinoboff