Forum Moderators: coopster

Message Too Old, No Replies

Making array walk array reusable?

After applying it the function's effects aren't retained!

         

JAB Creations

11:27 pm on Nov 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've successfully applied PHP's array_walk function to an array though only temporarily. The issue is PHP isn't actually applying the function's effects to the array...nor have I figured out how to save the effects to a new array...so in effect I have no idea how to use array_walk in a reusable manner! This means I'd have to apply it once each time I want to use the array...in the same file!

Here is what I have...

- John

<?php
$fruits = array("Lemony & Fresh","Orange Twist","Apple Juice");

$test = array_walk($fruits, 'name_base');
$fruits_fixed = array($test);

function name_base($key)
{
$name2 = str_replace(" ", "_", $key);
$name3 = str_replace("&", "and", $name2);
$name4 = strtolower($name3);
echo $name4.'<br />';
return $name4;
}
echo '<br />';
print_r($fruits_fixed);
echo '<br /><br />';
if (is_array($fruits_fixed)) {echo 'is array';}
else {echo 'not array';}
echo '<br /><br />';

var_dump($fruits_fixed);
?>

JAB Creations

11:52 pm on Nov 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Resolved! The array_walk function seems completely useless...and with only minimal modifications array_map was able to do the job just fine!

- John

<?php
$fruits = array("Lemony & Fresh","Orange Twist","Apple Juice");

print_r($fruits);
echo '<br />';

function name_base($key)
{
$name2 = str_replace(" ", "_", $key);
$name3 = str_replace("&", "and", $name2);
$name4 = strtolower($name3);
echo $name4.'<br />';
return $name4;
}
echo '<br />';

$test = array_map('name_base', $fruits);
$fruits_fixed = $test;
echo '<br />';
print_r($fruits_fixed);
?>