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 Quote 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. Quote 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; } } ?> Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.