1Flatten multidimensional associative array to array
2
3function flatten(array $array, $prefix="") {
4 $result = Array();
5 array_walk($array, function ($value, $key) use ($array, $prefix, &$result) {
6 $path = $prefix ? "$prefix.$key" : $key;
7 if (is_array($value)) {
8 $result = array_merge($result, flatten($value, $path));
9 } else {
10 $result[$path] = $value;
11 }
12 });
13
14 return $result;
15}
16
17print_r(flatten($arrr));
18
19from site:
20https://stackoverflow.com/questions/39071537/array-walk-recursive-to-return-array-name-rather-than-index-number
1<?php
2/*
3* Two Dimensional Arrays:- array_walk_recursive() function is used
4**/
5$alphabets = array(
6 'a' => 'apple',
7 'b' => 'ball',
8 'c' => 'cat',
9 array(
10 'd' => 'dog',
11 'e' => 'elephant',
12 )
13);
14
15array_walk_recursive($alphabets, 'myFunc2', 'for' );
16
17function myFunc2($value, $key, $param){
18 echo "$key $param $value <br>";
19};
20?>
21/*
22
23Out Put
24a for apple
25
26b for ball
27
28c for cat
29
30d for dog
31
32e for elephant
33*/
1$arr = array(1, array(2, 3));
2// Call function on every item, even subarrays.
3// Sign $item as reference to work on original item.
4array_walk_recursive($arr, function(&$item, $key, $myParam){
5 $item *= 2;
6}, 'will be in myParam');
7// $arr now is [2, [4, 6]]