Unset not working in multiple foreach statements (PHP) -


can tell me wrong code :

foreach ($data $region ):    foreach ($region $type):     foreach ($type $type2):      foreach ($type2 $key=>$val):      if ($val=='background-color: ffffff;' || $val=='')  unset($type2[$key]);      endforeach;     endforeach;    endforeach;  endforeach; 

after print_r($data) seems data array same , unset not working

your loop operating on copies of original elements; changes $type2 not visible in $data because $type2 copy.

you can solve iterating on arrays key, indexing $data keys remove value:

foreach ($data $k1 => $region ):    foreach ($region $k2 => $type):     foreach ($type $k3 => $type2):      foreach ($type2 $k4 =>$val):      if ($val=='background-color: ffffff;' || $val=='') {         unset($data[$k1][$k2][$k3][$k4]);     }      endforeach;     endforeach;    endforeach;  endforeach; 

of course ugly, that's 4 nested loops do. there option if iterating reference instead of grabbing keys, dislike because of nice opportunity write bugs reusing abandoned references after loop has ended. in case dislike fourth power.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -