php - Foreach and unset() -


ran little snag , wondering if there "best practices" way around it.

so learned "a php foreach execute on entire array regardless. test unsetting value next in iteration. iterate on offset, value null. – kevin peno dec 22 '09 @ 21:31" how remove array element in foreach loop?

it's first part of that messing me. i'm iterating through array foreach. it's search function i'm removing element searched for, when loop runs again minus element.

i not want reindex if @ possible, although if have can.

array ( [0] => array     (         [0] =>         [1] => aa         [2] => aaa     )  [1] => array     (         [0] => b         [1] => bb         [2] => bbb     )  [2] => array     (         [0] => c         [1] => cc         [2] => ccc     )  [3] => array     (         [0] => d         [1] => dd         [2] => ddd     ) 

)

foreach($array $key=>$value) {    $searchresult[] = search function returns various other keys array     foreach($searchresult $deletionid) {       unset($array[$deletionid]);    }  } 

so on first iteration uses $array[0] $searchresults might return 4,5,6,7. keys removed $array.

yet foreach loop still iterates through , gives me bunch of empty arrays.

i did read how php 'foreach' work? , of it.

thanks

in opinion, best way remove array elements based on indexes use array_* set of functions, array_diff , array_intersect (or array_diff_key , array_intersect_key in situation).

$indexes_to_remove = array(2,3,4); $indexes_to_remove = array_flip($indexes_to_remove);  $array = array_diff_key($array,$indexes_to_remove); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -