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
1
vote
1 answer

increment an integer in a array_walk() in PHP

I need to use an incremented value for the index orders2 of the array and I have tried the following: $i = 0; array_walk($arr1, function(&$a) { $i++; $a['orders2'] = $i; }); Which says $i is unknown in the line $i++;. I know I can use foreach()…
M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76
1
vote
1 answer

PHP: Build a URL encoded while a value exists in multi array

My goal is: If a value in those arrays match with an extention of a file, then I can builds a nice URL encoded string which I can append to a url using the http_build_query() function. If no match is found, it return nothing. I have tried it every…
1
vote
3 answers

PHP recursive issue

Hi I am trying to find the longest string in the following array: $niza = array( array(5, 1.15 , 15), array('unknown', 0.75 , 5, array(2, 'bla', 1.15) ), array(array('text'), 1.15 , 7) I am using the following function but I dont get the desired…
Nasi Jofce
  • 132
  • 2
  • 10
1
vote
4 answers

PHP sum up array entries where two keys have the same value

I have the following array, in which I want to sum up the total_volume for all entries where the source and the target are the same. Array ( [0] => Array ( [source] => ABC [target] => DEF [total_volume] => 10 ) [1]…
mxzwrnz
  • 29
  • 6
1
vote
0 answers

Array to string conversion with array_walk in PHP

I am tried to add the same string to every array value. I tried to use array_walk() like I read it on this answer. But I get: Notice: Array to string conversion I have also tried to use array_map(), but I get same error notice. working code if…
Grischa
  • 70
  • 8
  • 26
1
vote
1 answer

how can add special characters to keys of array

I want add special character to keys of my array, like this example. how can i convert this array $tmp = array( "name" => "aya", "number" => "10"); to $tmp = array( "[name]" => "aya", "[number]" => "10");
aya
  • 1,597
  • 4
  • 29
  • 59
1
vote
1 answer

What am I doing wrong with array_walk (and is there a better way)?

I have an array called "methods" and it looks like so: [0] => Array ( [id] => WWAM [title] => WWAM [cost] => 4.35 ) [1] => Array ( [id] => CNQM [title] => CNQM [cost] => 5.21 ) [2] => Array ( [id] => CNAM …
bcsteeve
  • 973
  • 9
  • 22
1
vote
1 answer

PHP array_walk() in class with multiple arguments

Good day, I got the following issue. Have a method in a class that I want to call with array_walk with two arguments. array_walk($fields, array($this, 'SetAlias'), $Table); When I put a comment in the method SetAlias() it does respond. Hence it is…
Alex
  • 1,223
  • 1
  • 19
  • 31
1
vote
1 answer

How to use array_walk() with a function which belongs to a class?

I'm trying to apply a function to every item in an array in PHP. However, this function is contained in another file, containing a class. I have included the file, and the function can be called easily enough. It just won't work with array_walk.…
Allenph
  • 1,875
  • 27
  • 46
1
vote
3 answers

adding string keys to inner arrays

I wish to add string keys to my inner PHP arrays. So, I want to convert this: array (size=2) 0 => array (size=3) 0 => string 'X705' (length=4) 1 => string 'X723' (length=4) 2 => string 'Sue' (length=0) 1 => array (size=3) 0 => string 'X714'…
Karma Kaos
  • 69
  • 13
0
votes
1 answer

walk through array with nested output in PHP

I have this code: foreach ($_POST as $key1 => $item1): if (is_array($item1)): foreach ($item1 as $key2 => $item2): if (is_array($item2)): foreach ($item2 as $key3 => $item3): if…
Milad
  • 51
  • 9
0
votes
1 answer

Put values in array outside of array_walk_recursive()

I want to recursively look through a nested JSON object for keys called 'image', and push their values (URL strings) into another array outside the function. From other examples and SO questions, I understand that I need to pass a reference to the…
Rhecil Codes
  • 511
  • 4
  • 24
0
votes
0 answers

How to pass multiple parameters in array_walk

I want to use array_walk so I am passing extra two array as parameter but didn't get exact process to deal it? o how to pass extra parameters in the form of array and access inside array_walk $main_array; $array_1; $array_2; …
user_1234
  • 741
  • 1
  • 9
  • 22
0
votes
1 answer

PHP Merge multiple multi-dimensional, also keep unique children

Sorry for such a long description but I'm struggling with the merging of multiple multi-dimensional associative arrays for more than a day now. Can anyone please suggest the optimized way to achieve this. I want to create HTML listing based on…
0
votes
1 answer

array_walk not changing value

function values($id,$col) { $vals = [1=>['name'=>'Lifting Heavy Boxes']]; return $vals[$id][$col]; } $complete = [1=>["id"=>"2","sid"=>"35","material_completed"=>"1","date"=>"2017-12-18"]]; $form = 'my_form'; array_walk($complete,…
Works for a Living
  • 1,262
  • 2
  • 19
  • 44