Isityou Posted February 13, 2008 Share Posted February 13, 2008 I have a two dimensional array that contains data. I want to loop through the two dimensional array and sanitize each element with htmlentities(), how would I go about doing that? Link to comment https://forums.phpfreaks.com/topic/90942-loop-through-two-dimensional-array/ Share on other sites More sharing options...
schilly Posted February 13, 2008 Share Posted February 13, 2008 Use nested while/for loops. $i = 0; while($arr = $array[$i]){ $j = 0; while($nextarr = $arr[$j]){ //operate on value here $j++; } $i++; } Link to comment https://forums.phpfreaks.com/topic/90942-loop-through-two-dimensional-array/#findComment-466092 Share on other sites More sharing options...
laffin Posted February 13, 2008 Share Posted February 13, 2008 ya can take lots of different ways. $td = array( array('%','&'), array('<','>'), array(5,6) ); the simplest is using foreach 2 levels foreach($td as $key => $sd) { foreach($sd as $key => $item) { $sd[$key]=htmlentities($item); } } Link to comment https://forums.phpfreaks.com/topic/90942-loop-through-two-dimensional-array/#findComment-466095 Share on other sites More sharing options...
Isityou Posted February 13, 2008 Author Share Posted February 13, 2008 Thanks . I nested foreach loops as you showed me. Link to comment https://forums.phpfreaks.com/topic/90942-loop-through-two-dimensional-array/#findComment-466102 Share on other sites More sharing options...
sasa Posted February 13, 2008 Share Posted February 13, 2008 try <?php function my_h(&$a){$a = htmlentities($a);} $td = array( array('%','&'), array('<','>'), array(5,6) ); array_walk_recursive($td, 'my_h'); print_r($td); ?> Link to comment https://forums.phpfreaks.com/topic/90942-loop-through-two-dimensional-array/#findComment-466147 Share on other sites More sharing options...
laffin Posted February 13, 2008 Share Posted February 13, 2008 LOL Very nice sasa I keep forgetting all the nice functions php provides Coming from a C world. Link to comment https://forums.phpfreaks.com/topic/90942-loop-through-two-dimensional-array/#findComment-466228 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.