n33dl3 Posted March 4, 2007 Share Posted March 4, 2007 Hi, I have an array with the following values in it: REC_DATE = 'abc' REC_CREATE = 'abc' REC_DONE = 'abc' DATE = 'abc' TEST = 'abc' Now I would like to put all the values which are starting with REC in to another array, what would be the best way to achieve this? Regards Link to comment https://forums.phpfreaks.com/topic/41100-solved-copy-values-from-array-in-to-new-array/ Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 Something like this? <?php $array = array("REC_DATE" => "abc", "REC_CREATE" => "abc", "REC_DONE" => "abc", "DATE" => "abc", "TEST" => "abc"); $rec = array(); foreach ($array as $key => $val) { if(strpos(strtolower($key), "rec_") === 0) { $rec[substr($key, 4)] = $val; unset($array[$key]); } } echo "<pre>"; print_r($array); echo "\n\n\n"; print_r($rec); echo "</pre>"; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/41100-solved-copy-values-from-array-in-to-new-array/#findComment-199045 Share on other sites More sharing options...
trq Posted March 4, 2007 Share Posted March 4, 2007 You meen all the values which key starts with REC? <?php foreach ($oldarray as $key => $val) { if (substr($key,0,3) == 'REC') { $newarray[$key] = $val; } } ?> Link to comment https://forums.phpfreaks.com/topic/41100-solved-copy-values-from-array-in-to-new-array/#findComment-199046 Share on other sites More sharing options...
n33dl3 Posted March 4, 2007 Author Share Posted March 4, 2007 Yeah exactly Thnx for helping Regards Link to comment https://forums.phpfreaks.com/topic/41100-solved-copy-values-from-array-in-to-new-array/#findComment-199048 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.