note 76533 deleted from function.array-map by cmb

From: Date: Sun, 25 Aug 2019 09:10:06 +0000
Subject: note 76533 deleted from function.array-map by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212001@lists.php.net to get a copy of this message
Note Submitter: bturchik at iponweb dot net ---- Maybe this one will be useful for someone: function array_map_helper($mapper, $array) { $mapper = preg_replace('/^return (.*?);$/', '$1', trim($mapper)); $result = array(); if (preg_match('/(\(?)(.*?)\s*=>\s*(.*?)(\)?)$/', $mapper, $matches)) { list($full_found, $array_open, $left, $right, $array_close) = $matches; if ($array_open && $array_close) { $mapper = '$result[] = array' . $full_found . ';'; } else { $mapper = '$result[' . $left . '] = ' . $right . ';'; } } else { $mapper = '$result[] = ' . $mapper . ';'; } foreach ($array as $key => $value) { eval($mapper); } return $result; } should be used like: $array = array(array('foo' => 11, 'bar' => 22), array('foo' => 111, 'bar' => 222), array('foo' => 1111, 'bar' => 2222)); $mapped = array_map_helper('$value["foo"] => $value["bar"]', $array); var_dump will give array(3) { [11]=> int(22) [111]=> int(222) [1111]=> int(2222) } or $mapped = array_map_helper('$value["foo"]', $array); var_dump will give array(3) { [0]=> int(11) [1]=> int(111) [2]=> int(1111) } or $mapped = array_map_helper('$value["foo"] + $value["bar"] . " at position $key"', $array); var_dump will give array(3) { [0]=> string(16) "33 at position 0" [1]=> string(17) "333 at position 1" [2]=> string(18) "3333 at position 2" }

« previous php.notes (#212001) next »