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
47
votes
5 answers

How do I use a class method as a callback function?

If I use array_walk inside a class function to call another function of the same class class user { public function getUserFields($userIdsArray,$fieldsArray) { if((isNonEmptyArray($userIdsArray)) && (isNonEmptyArray($fieldsArray))) …
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
16
votes
3 answers

Why won't trim work as a callback for array_walk or array_map in PHP?

Why does my sample code result in the first string still having a trailing space? $a=array('test_data_1 ','test_data_2'); array_walk($a, 'trim'); array_map('trim', $a); foreach($a AS $b){ var_dump($b); } string(12)…
Jaak Kütt
  • 2,566
  • 4
  • 31
  • 39
13
votes
2 answers

Cannot iterate over iterator with array_walk since php 7.4

$iterator = new ArrayIterator([1, 2]); array_walk($iterator, function($item) {echo $item . PHP_EOL;}); This piece of php code outputs the items (1 and 2) in php 7.3 but it outputs nothing in php 7.4. Can anyone explain what changed in php 7.4 that…
Tadas
  • 331
  • 1
  • 7
5
votes
0 answers

Strict_types does not affect array_walk

While working on the following code I noticed that declare(strict_types=1) has no effect on the arguments of a callback function through array_walk()
Rain
  • 3,416
  • 3
  • 24
  • 40
5
votes
2 answers

using array_walk_recursive() for stdClass objects

I have looked through a few answers on here but that don't seem to utilise this method? I have an array of items, and the items are objects. The object can have a key which is 'children' and 'children' is an array of objects etc. Is there a way to…
Ryan Hipkiss
  • 648
  • 1
  • 7
  • 20
4
votes
4 answers

How do I use the PHP array_walk function to rearrange keys in a multidimensional array?

I have a PHP array in the following format: array (size=2) '2019-09-28' => array (size=2) 'A' => float 4 'B' => float 85 'C' => float 200 '2019-09-29' => array (size=2) 'A' => float 5 'B' => float 83 …
4
votes
5 answers

How to access others variable inside of array_walk?

I have a variable $id = 10, it's need to use inside of array_walk(). Like bellow : $id = 10; array_walk($profile_items, function(&$a) { $count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id',…
Razib Al Mamun
  • 2,663
  • 1
  • 16
  • 24
3
votes
3 answers

Greater than or Smaller than within array_walk recursive

I am trying to replace array value with specific condition. Suppose array $mark has following value $mark = array(90,85); Just normal checking value it is echoing correct value array_walk_recursive($mark, function (&$k) { if($k==90){$k='4.0';} …
Hemant
  • 83
  • 3
  • 9
3
votes
3 answers

array_walk trying to write to outside array

I have a simple piece of code that isn't working as I would expect it would, could someone please explain why it isn't populating the fields array and how to solve it. $fields = []; array_walk($class->properties, function($v, $k) use ($fields) { …
user3791372
  • 4,445
  • 6
  • 44
  • 78
3
votes
2 answers

What does array_walk() do?

I don't get what array_walk($arr, 'intval'); does, which I have commented out in the following code snippet:
Anshu Shekhar
  • 105
  • 2
  • 10
2
votes
1 answer

Push key value in empty array using php

I have associative array and value related with that key contain json_encoded data so I converted it and it resulted in array,I am using array_walk to iterate each array value than and printing values using foreach loop but at same time I want to…
user_1234
  • 741
  • 1
  • 9
  • 22
2
votes
4 answers

How do I get the value of the first occurrence of array_walk_recursive in php

I have a deep multidimensional array that I am needing to extract the value of a specific key. I have found that the array_walk_recursive function will be my best option. I only need the first occurrence. My array looks like this - (except much more…
codyfraley
  • 99
  • 3
  • 12
2
votes
1 answer

PHP Can't print all keys name in array with array_walk()

My gallery folder has this structure: /YEAR/MONTH/DAY/FILE Then, i am going to print them in my website. I have a function that gets all files in a big array, with this struct: Array ( [2017] => Array ( [01] => Array …
viher
  • 114
  • 1
  • 9
2
votes
2 answers

array_walk with anonymous function

I am getting familiar with anonymous function and closures in php and I need to use a closure or anon function to pass to array_walk but with an additional parameter here is a simple code block: $array = array(1, 2, 3, 4, 5, array(1, 2)); …
Combinu
  • 882
  • 2
  • 10
  • 31
1
2 3 4