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

PHP 5.4 Call-time pass-by-reference - How to fix it?

I have the problem that I get the error PHP Fatal error: Call-time pass-by-reference has been removed in.... I discovered some similar questions e.g. PHP 5.4 Call-time pass-by-reference - Easy fix available? Call-time pass-by-reference…
Radon8472
  • 4,285
  • 1
  • 33
  • 41
0
votes
1 answer

Display array result in html table using array_walk or array_map

I am working with users data inside array and I want to print user data inside html table without using foreach loop but alternative of that using array_walk() db->get('user')->result(); echo…
Anonymous
  • 1,074
  • 3
  • 13
  • 37
0
votes
1 answer

Geeting error Undefined variable: desc even already defined as empty

I want to print all user names one by one which is array format, so instead of using foreach loop I am using array_walk() and it is printing all user data one by one but as per requirement data should be print in html table so defined one varaible…
MA Samad
  • 73
  • 6
0
votes
1 answer

Assigning multiple associative to one

I am trying to add a question and I have 4 options for that question. Among those options, only one will be the correct of course. But, after submitting the form, I am receiving the data like the following: Array ( [option_A] => answera …
0
votes
2 answers

how to terminate array_walk on first loop?

I need to print only one sub_title of first child(1955) array. Here is some sample code (that works) to show what I mean. $array = Array ( [1955] => Array ( [sub_title] => subtitle [sub_content] => content …
0
votes
1 answer

Replace an element with a specific key in a multidimensional array of unknown depth

I have an array in PHP. The array could be X deep. Somewhere in the array is an object with key custom_image. I want to replace the object with my own data. See attached screenshot for example of structure. Note the custom_image is often times at a…
Drew Baker
  • 14,154
  • 15
  • 58
  • 97
0
votes
2 answers

PHP Array Goup using date with multiple keys and assign Sum of keys

I'm wring a PHP script to group some payment info using date. part of original array is like this. array(50) { [0]=> array(2) { ["Datum"]=> string(10) "2016-07-07" ["C"]=> int(1) } [1]=> array(2) { ["Datum"]=> …
0
votes
2 answers

How to get a PHP function alike array_walk that will return an array?

Is there any built-in function existing in PHP alike array_walk() that will return an array instead of true or false? For information, I am trying the following code but in the result of the code getting OR at the end of the string I need to remove…
Bablu Ahmed
  • 4,412
  • 5
  • 49
  • 64
0
votes
1 answer

PHP Multidimensional array flattening

I have the following input array, I would like to flatten the array to evaluate true or false conditions. array ( 'all' => array ( '0' => array ( 0 => 'true') '1' => array ( 0 => 'true') …
0
votes
0 answers

Why those nearly the same codes get a different result in php's array_walk + ksort?

I want to sort a multi-array by key, those data come form front end somehow will be mess up in an random sort. So I must make the data sorted, at first I natural use the combine 'array_walk($array, "ksort")'. Work fine at simple array data, while…
ccbility
  • 25
  • 3
0
votes
1 answer

Get current index in array_walk function

$A = ["0" => "a", "1" => "b", "2" => "c"]; $B = ["0" => "aa", "1" => "bb", "2" => "cc"] array_walk($A,function($item) use($B){ $temp[] = $item; $temp[] = $B[?]; }); how to fill the ? above? how to get the current index in array_walk?
Jin Aazoe
  • 115
  • 1
  • 1
  • 9
0
votes
4 answers

Combining two arrays based on values php

I was wondering, how it is possible to perform function on each array element based on value. For example, if I have two arrays: [ 0 => 'gp', 1 => 'mnp', 2 => 'pl', 3 => 'reg' ] And $translation = [ 'gp' => 'One', 'mnp' =>…
Kristīne Glode
  • 1,409
  • 4
  • 16
  • 22
0
votes
1 answer

array_walk not doing the walk for me

When I pass an array as an extra argument to array walk, it does not get interpreted as I would expect. function boom($item, $z ) { print_r("\n".$item); print_r("\n".$z); } $z=[ "alpha", "bravo" ]; $x=[ "one", "two" ]; array_walk( $x,…
user4278933
0
votes
1 answer

Php array_walk with anonymous function for filtering out results

I have a big set of time records for a project and I want to filter out all but those posted by a single employee. array_walk($timeRecords, function($timeRecord, $index) use ($employee) { if ($timeRecord->employeeId != $employee->id) { …
JamesNZ
  • 482
  • 5
  • 17
0
votes
1 answer

PHP Why does array_walk not work with DOMDocument::getElementsByTagName

just wondering why the following code doesn't itterate through DOMDocument::getElementsByTagName preserveWhiteSpace = false; $dom->loadHTML('...blablabla...'); $elements =…