UpcomingPhpDev Posted December 23, 2008 Share Posted December 23, 2008 Hey again, Yes, It seems I am having another problem with multi dimensional arrays. Basically, I want to strip html, trim, and rmeove whitespace characters from my Array Array's Ive tried, array_walk,Array_map, Array_walk_recursive, Plus other self built functions, But I just cant do it. My array keys arnt numeric. Heres an example code to work with, Hope some1 here maybe able to solve it. Thanks <?php $Array = array(array(" RedArrow", "<p>Php)); print "<PRE>"; print_r($Array); print "</PRE>"; ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 You should be able to use either walk or map to traverse through the multi-dimensional array and apply those functions. The only other way would be: $arr = array( array(" RedArrow", " Php")); foreach($arr as $key1 => $value1) { array_walk($value1, 'trim'); array_walk($value1, 'strip_tags'); } foreach($arr as $key1 => $value1) { foreach($value1 as $key2 => $value2) { echo $key2 . "=> " . strip_tags(trim($value2)) . " "; } } Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 29, 2008 Author Share Posted December 29, 2008 You should be able to use either walk or map to traverse through the multi-dimensional array and apply those functions. The only other way would be: <?php $arr = array( array(" RedArrow", "<p>Php")); foreach($arr as $key1 => $value1) { array_walk($value1, 'trim'); array_walk($value1, 'strip_tags'); } foreach($arr as $key1 => $value1) { foreach($value1 as $key2 => $value2) { echo $key2 . "=> " . strip_tags(trim($value2)) . "<br />"; } } No this still isnt working, Have yo actually tested your code? I tried this before I belive as its logical to do so. Thanks Quote Link to comment Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 Yes, my output is: 0=> RedArrow 1=> Php Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 29, 2008 Share Posted December 29, 2008 <?php function clean(&$item) { $item = strip_tags(trim($item)); } $input = array( array(" RedArrow ", "<p>Php")); array_walk_recursive($input, 'clean'); print_r($input); ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 Seems like flyhoney's solution works as well. Output from flyhoney's: Array ( [0] => Array ( [0] => RedArrow [1] => Php ) ) (don't know why it bullets my array I guess the "[ 0]" = a bullet point?) Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 29, 2008 Author Share Posted December 29, 2008 Yes, my output is: 0=> RedArrow 1=> Php Dont know whats wrong my side then, Thanks still <?php function clean(&$item) { $item = strip_tags(trim($item)); } $input = array( array(" RedArrow ", "<p>Php")); array_walk_recursive($input, 'clean'); print_r($input); ?> Yeh this works. Thanks! forgot about the recursive feature Quote Link to comment Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 Ive tried, array_walk,Array_map, Array_walk_recursive, Plus other self built functions, But I just cant do it. I thought you said you tried it Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 29, 2008 Author Share Posted December 29, 2008 Ive tried, array_walk,Array_map, Array_walk_recursive, Plus other self built functions, But I just cant do it. I thought you said you tried it Yeh seems I did say that, Sorry, If I did try that I would have used it wrongly. Thanks for ur help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.