Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php array_walk

$arr = array(1, 2, 3);
// Call function on every item.
// Sign $item as reference to work on original item.
array_walk($arr, function(&$item, $key, $myParam){
  $item *= 2;
}, 'will be in myParam');
// $arr now is [2, 4, 6]
Comment

array_walk in php

array_walk():
This array function visits each element of array in the user defined function. 
The array's keys and values are parameters in the function, 
This function will not return value.
Also it can have 3'rd parameter as optional.

<?php

  function funTable($val, $key){
	  print $val.'==='.$key."
";
	}
	
	$arrWalk = array(1=>2,2=>2,3=>2,4=>2,5=>2);
	//$arrWalk = array(1,2,3,4,5,6,7,8,9,10);
	array_walk($arrWalk,'funTable');
	
?>
======================
// to replace all the values needs to add parameter with &
<?php

  function funTable1(&$val, $key, $override=null){
	   if($override)
  	   $val = 5;
	}
	
	$arrWalk = array(1=>2,2=>2,3=>2,4=>2,5=>2);
	//$arrWalk = array(1,2,3,4,5,6,7,8,9,10);
	array_walk($arrWalk,'funTable1',0);
	//array_walk($arrWalk,'funTable1',1);
	
	print_r($arrWalk);
	
?>
Comment

php array_walk

PHP function array_walk(object|array &$array, callable $callback, mixed $arg) bool
------------------------------------------------------------------------------  
Apply a user function to every member of an array.
  
Parameters:
array|object--$array--The input array.
callable--$callback--Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second.
If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference. Then, any changes made to those elements will be made in the original array itself.
Users may not change the array itself from the callback function. e.g. Add/delete elements, unset elements, etc. If the array that array_walk is applied to is changed, the behavior of this function is undefined, and unpredictable.
mixed--$arg--[optional] If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback funcname.

Returns: true on success or false on failure.
Comment

PREVIOUS NEXT
Code Example
Php :: laravel collection max 
Php :: php excel to array 
Php :: how to return chunk data laravel 
Php :: laravel mail send 
Php :: rename migration laravel 
Php :: laravel @disabled in laravel-9 
Php :: what does defined di in php 
Php :: enqueue css 
Php :: how make a variable global php 
Php :: move_uploaded_file 
Php :: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) laravel 
Php :: how to merge 2 laravel eloquent records 
Php :: laravel collection sort 
Php :: how to acces sql with php 
Php :: laravel migrations generator laravel 
Php :: php octal to decimal 
Php :: laravel attach 
Php :: php if short form 
Php :: https://www.60d48b1061adf.site123/wp-login.php 
Php :: foreign key cosntraint laravel 
Php :: how to make a loop that adds numbers to a variable in php 
Php :: codeigniter 3 or where in 
Php :: php build query from array 
Php :: How to add custom button in wordpress admin section 
Php :: get taxonomy name in taxonomy page wordpress dev 
Php :: create a laravel project 
Php :: php add new item to associative array 
Php :: php random filename generator 
Php :: PHP str_repeat — Repeat a string 
Php :: php dies while parsing json 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =