<?php ArrayUtil::ForEachFn($array, function (&$key, &$value) { // YOUR CODE HERE });To use this technique in your project simply add the following class:
<?php class ArrayUtil { public static function ForEachFn(Array &$array, $fn) { $newArray = array(); foreach ($array as $key => $value) { $fn($key, $value); $newArray[$key] = $value; } $array = $newArray; } }To see it all in action checkout the following example:
<?php // EXAMPLE ARRAY $array = array( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', ); // BEFORE print_r($array); // EXAMPLE USAGE ArrayUtil::ForEachFn($array, function (&$key, &$value) { // NOTE THE FUNCTION'S SIGNATURE (USING & FOR BOTH $key AND $value) if ($key === 'key2') { $key = 'BY REF KEY EXAMPLE'; // THIS IS THE WHOLE POINT OF THIS POST } if ($value === 'value2') { $value = 'BY REF VALUE EXAMPLE'; } }); // AFTER print_r($array); /* THE ABOVE "EXAMPLE USAGE" OUTPUTS: Array ( [key1] => value1 [key2] => value2 [key3] => value3 ) Array ( [key1] => value1 [BY REF KEY EXAMPLE] => BY REF VALUE EXAMPLE [key3] => value3 ) */
Looking for a cloud-based catalog and CRM platform? Checkout http://rocware.com/
great blog thanks for information
ReplyDeletenine pyramid plate
Deepam Oil
excellent blog thanks for information
ReplyDeleteDeepam Oil