array_walk

We can create our own function and pass each element of an array through the function.

array_walk(array, function )

Let us try this with an example. We will try to add one char 'x' to each element of an array

<?Php
function add_char($str){
$str = $str.'x';
echo $str.'<br>';
}
$a= array(1,2,v,4,7.3,105 );

array_walk($a, 'add_char');
?>

The output is here

1x
2x
vx
4x
7.3x
105x

The function array_walk returns TRUE or FALSE based on the success or failure. It does not return a new array with modified elements. You can add the following lines to test your script.

<?Php
function add_char($str){
$str = $str.'x';
echo $str.'<br>';
}
$a= array(1,2,v,4,7.3,105 );

if(array_walk($a, 'add_char')){
echo " Success ";
}else{
echo " Failed";
}
?>

The output is here

1x
2x
vx
4x
7.3x
105x
Success

To get a new array with modified elements then we have to use array_map function.

array_map function to get a new array by applying callback function

Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer