Jump to content

[SOLVED] How to apply functions or TRIM Multi Dimensional Arrays


UpcomingPhpDev

Recommended Posts

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>";
?>

Link to comment
Share on other sites

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)) . "
";
}
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.