note 76533 added to function.array-map

From: Date: Thu, 19 Jul 2007 14:46:15 +0000
Subject: note 76533 added to function.array-map
Groups: php.notes 
Request: Send a blank email to php-notes+get-128727@lists.php.net to get a copy of this message
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" } ---- Server IP: 69.147.83.197 Probable Submitter: 89.175.29.5 ---- Manual Page -- http://www.php.net/manual/en/function.array-map.php Edit -- https://master.php.net/note/edit/76533 Del: integrated -- https://master.php.net/note/delete/76533/integrated Del: useless -- https://master.php.net/note/delete/76533/useless Del: bad code -- https://master.php.net/note/delete/76533/bad+code Del: spam -- https://master.php.net/note/delete/76533/spam Del: non-english -- https://master.php.net/note/delete/76533/non-english Del: in docs -- https://master.php.net/note/delete/76533/in+docs Del: other reasons-- https://master.php.net/note/delete/76533 Reject -- https://master.php.net/note/reject/76533 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128727) next »