Answers for "php array_walk"

PHP
0

php array_walk

$arr = array(1, 2, 3);
// Call function on every item.
// Sign $item as reference to work on original item.
array_walk($arr, function(&$item, $key, $myParam){
  $item *= 2;
}, 'will be in myParam');
// $arr now is [2, 4, 6]
Posted by: Guest on May-11-2021
0

php array walk

<?php
/*
*   For One Dimensional Array
*/
$alphabets = array(
    'a'  =>  'apple',
    'b'  =>  'ball',
    'c' =>  'cat',
);

array_walk($alphabets, 'myFunc', 'for');

function myFunc($value, $key, $param)
{
    echo "$key $param $value <br>";
}
?>
/*
Out Put:- 
a for apple
b for ball
c for cat
*/
 <?php
/*
*   For Two Dimensional Arrays:-  array_walk_recursive() 
**/
$alphabets = array(
    'a'  =>  'apple',
    'b'  =>  'ball',
    'c' =>  'cat',
    array(
        'd' =>  'dog',
        'e' =>  'elephant',
    )
);

array_walk_recursive($alphabets, 'myFunc2', 'for' );

function myFunc2($value, $key, $param){
    echo "$key $param $value <br><br>";
};
?>
Posted by: Guest on May-29-2021
0

php array_walk

PHP function array_walk(object|array &$array, callable $callback, mixed $arg) bool
------------------------------------------------------------------------------  
Apply a user function to every member of an array.
  
Parameters:
array|object--$array--The input array.
callable--$callback--Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second.
If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference. Then, any changes made to those elements will be made in the original array itself.
Users may not change the array itself from the callback function. e.g. Add/delete elements, unset elements, etc. If the array that array_walk is applied to is changed, the behavior of this function is undefined, and unpredictable.
mixed--$arg--[optional] If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback funcname.

Returns: true on success or false on failure.
Posted by: Guest on September-11-2021

Browse Popular Code Answers by Language