Questions tagged [array-walk]

A PHP function that walks an array and applies a provided function on the array elements.

A PHP function that walks an array and applies a provided function on the array elements.

bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )

Applies the user-defined callback function to each element of the array array. manual

array_walk() is not affected by the internal array pointer of array. array_walk() will walk through the entire array regardless of pointer position.

Example

$fruits = array("lemon", "orange", "banana", "apple");

function test_alter(&$item1, $key, $prefix){
    $item1 = "$prefix: $item1";
}
array_walk($fruits, 'test_alter', 'fruit');

print_r($fruits);

Result

Array
(
    [0] => fruit: lemon
    [1] => fruit: orange
    [2] => fruit: banana
    [3] => fruit: apple
)
55 questions
0
votes
1 answer

Anonymous function breaking when upgrading PHP

The following code is suddenly breaking when I switched servers/upgrading from PHP 5.3 to 5.4: function arrayValRecursive($key, array $arr, $string=false){ $val = array(); array_walk_recursive($arr, function($v, $k) use($key, &$val){ …
John
  • 11,985
  • 3
  • 45
  • 60
0
votes
1 answer

Alter specific array values based on others in same "row"

I am trying to perform a transformation/calculation on values in a returned SQL object, based on other values in the same row. I cannot find a good reference for this, perhaps I just don't know what I'm looking for; it seems most functions (e.g…
Wavelength
  • 18
  • 3
-1
votes
1 answer

PHP - modify formatted array

I have a function for arrays that I insert in db. $data = array( "This\tIs\tAn\tExample\tOne\tOf\tMy\tArrays\" ); I formatted it correctly and given output is with: $result = array_walk($data, function(&$item) { $item = '{ ' .str_replace("\t", ',…
Elementor
  • 57
  • 7
-1
votes
1 answer

PHP searching a multidemisional array by key

I would like to search through a multidimensional array and return the value of the first key found. For example I am working with some geo tools and it returns this array. How can I recursively go through this array and return the first value of…
John
  • 9,840
  • 26
  • 91
  • 137
-1
votes
2 answers

Php Array Zipper - add two arrays by key value

I am trying to do this as efficiently as possible. I have multiple arrays: array1 = [ "2018" => [ "JAN" => 100, "FEB" => 200, "MAR" => 300, "APR" => 400 ] ] array2 = [ "2018" => [ "FEB" =>…
Cybergei
  • 39
  • 1
  • 7
-1
votes
2 answers

Updating a value in a multidimensionnal array regarding another value and using array_walk (PHP)

I would like to update the values of a multidimensionnal php array : $a array(1) { ["test"]=> array(4) { [0]=> int(5) [1]=> int(7) [2]=> int(10) [3]=> int(14) } } For $a["test"][$i], I would like to obtain this new value $a["test"][$i] -…
user1719210
  • 310
  • 3
  • 11
-1
votes
2 answers

Is there any PHP array_* function for extracting single values from a multidimensional array?

If I have a multidimensional array, and I want to extract some of the data from it and place them in a new array, is there any existing array_*() function to do so? For example, if I have the following array: array( [ 'id' => 1, …
Magnus
  • 17,157
  • 19
  • 104
  • 189
-1
votes
1 answer

array_walk_recursive to return array name rather than index number

is there a way for the array_Walk_recursive to return the name of the array instead of index? function flatten(array $array) { $return = array(); array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; }); return…
Jason Lee
  • 43
  • 9
-1
votes
2 answers

array_walk only partially removes matches

Ran into a weird situation were using array_walk() will only partially remove matches from my method, not certain exactly what is going on. I am currently using PHP v5.6.4. The issue almost seems to be that it is only removing every secondary…
ehime
  • 8,025
  • 14
  • 51
  • 110
-2
votes
2 answers

How to apply a function with multiple arguments to every element of an array in PHP?

I have a method MyClass#foo(array $array, $argX, $argY, $argZ) and want to apply it to every element of $myArray. I've tried this with array_map(...) and array_walk(...), but they are not flexible enough for a function with a custom signature. How…
automatix
  • 14,018
  • 26
  • 105
  • 230
1 2 3
4