Page 1 of 1

[SOLVED!] Create array after applying array_walk function?

Posted: Wed Nov 26, 2008 4:20 pm
by JAB Creations
I was able to successfully apply the array_walk function to an array in this thread however to use the array after the function is applied more then once it would require using array_walk each time so I am trying to figure out how to permanently apply array_walk to an array or to assign it to a new array. I attempted to apply the array_walk to a new variable...presuming it might create an array out of the variable...in a funny way though after using the is_array function and testing the variable/array with print_r I'm not sure what to do at the moment in time...so a little direction would be greatly appreciated so I can reuse the array without having to reprocess it each time I wish to use it!

Here is what I have tried...

Code: Select all

<?php
$fruits = array("Lemony & Fresh","Orange Twist","Apple Juice");
 
$fruits_fixed = array_walk($fruits, 'name_base');
 
function name_base($key)
{
 $name2 = str_replace(" ", "_", $key);
 $name3 = str_replace("&", "and", $name2);
 $name4 = strtolower($name3);
 //echo $name4.'<br />';
 return $name4;
}
 
print_r($fruits_fixed);
echo '<br />';
if (is_array($fruits_fixed)) {echo 'is array';}
else {echo 'not array';}
?>

Re: Create new array after applying array_walk function?

Posted: Wed Nov 26, 2008 4:46 pm
by JAB Creations
Here is my latest attempt because once I get this working I can finish the whole mess with the tags and post the blog software I've been writing for a critique soon thereafter. :mrgreen:

The following...

Code: Select all

<?php
$fruits = array("Lemony & Fresh","Orange Twist","Apple Juice");
 
$fruits_fixed = array(array_walk($fruits, 'name_base'));
 
function name_base($key)
{
 $name2 = str_replace(" ", "_", $key);
 $name3 = str_replace("&", "and", $name2);
 $name4 = strtolower($name3);
 //echo $name4.'<br />';
 return $name4;
}
 
print_r($fruits_fixed);
echo '<br />';
if (is_array($fruits_fixed)) {echo 'is array';}
else {echo 'not array';}
echo '<br />';
 
var_dump($fruits_fixed);
?>
...outputs this...
Array ( [0] => 1 )
is array
array(1) { [0]=> bool(true) }
...I was thinking I could probably through this through some sort of loop to apply the function to each key individually though again that would be cheap and I wouldn't learn anything from it. Thoughts please?

Re: Create new array after applying array_walk function?

Posted: Wed Nov 26, 2008 6:06 pm
by JAB Creations
Solved! The array_walk function seems completely useless...and with only minimal modifications array_map was able to do the job just fine!

Code: Select all

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