php array walk

Solutions on MaxInterview for php array walk by the best coders in the world

showing results for - "php array walk"
Chris
30 Sep 2017
1$arr = array(1, 2, 3);
2// Call function on every item.
3// Sign $item as reference to work on original item.
4array_walk($arr, function(&$item, $key, $myParam){
5  $item *= 2;
6}, 'will be in myParam');
7// $arr now is [2, 4, 6]
Alice
23 Jan 2017
1<?php
2/*
3*   For One Dimensional Array
4*/
5$alphabets = array(
6    'a'  =>  'apple',
7    'b'  =>  'ball',
8    'c' =>  'cat',
9);
10
11array_walk($alphabets, 'myFunc', 'for');
12
13function myFunc($value, $key, $param)
14{
15    echo "$key $param $value <br>";
16}
17?>
18/*
19Out Put:- 
20a for apple
21b for ball
22c for cat
23*/
24 <?php
25/*
26*   For Two Dimensional Arrays:-  array_walk_recursive() 
27**/
28$alphabets = array(
29    'a'  =>  'apple',
30    'b'  =>  'ball',
31    'c' =>  'cat',
32    array(
33        'd' =>  'dog',
34        'e' =>  'elephant',
35    )
36);
37
38array_walk_recursive($alphabets, 'myFunc2', 'for' );
39
40function myFunc2($value, $key, $param){
41    echo "$key $param $value <br><br>";
42};
43?>