Find min/max in a two dimensional array

Here’s one way to get the min and max values: $min = min(array_column($array, ‘Price’)); $max = max(array_column($array, ‘Price’)); To return the nested array for the min and max: $prices = array_column($array, ‘Price’); $min_array = $array[array_search(min($prices), $prices)]; $max_array = $array[array_search(max($prices), $prices)]; You could do each in one line since that looked like what you were trying … Read more

Access first level keys with array_map() without calling `array_keys()`

Not with array_map, as it doesn’t handle keys. array_walk does: $test_array = array(“first_key” => “first_value”, “second_key” => “second_value”); array_walk($test_array, function(&$a, $b) { $a = “$b loves $a”; }); var_dump($test_array); // array(2) { // [“first_key”]=> // string(27) “first_key loves first_value” // [“second_key”]=> // string(29) “second_key loves second_value” // } It does change the array given as … Read more

Performance of foreach, array_map with lambda and array_map with static function

Its interesting to run this benchmark with xdebug disabled, as xdebug adds quite a lot of overhead, esp to function calls. This is FGM’s script run using 5.6 With xdebug ForEach : 0.79232501983643 MapClosure: 4.1082420349121 MapNamed : 1.7884571552277 Without xdebug ForEach : 0.69830799102783 MapClosure: 0.78584599494934 MapNamed : 0.85125398635864 Here there is only a very small … Read more