esiason14 Posted May 26, 2007 Share Posted May 26, 2007 I'm looking to explode the comma separated values in an array like this: Array ( [0] => 10811 [1] => 7527,7556 ) and then add the exploded value as a new key in the array...like this: Array ( [0] => 10811 [1] => 7527 [2] => 7556 ) Quote Link to comment https://forums.phpfreaks.com/topic/53091-solved-exploding-comma-separated-in-array/ Share on other sites More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 and questions are asked like this Quote Link to comment https://forums.phpfreaks.com/topic/53091-solved-exploding-comma-separated-in-array/#findComment-262255 Share on other sites More sharing options...
esiason14 Posted May 26, 2007 Author Share Posted May 26, 2007 and questions are asked like this Ok, I gotcha...but I'm pretty sure you can understand my "stupid" question. My problem: I have some arrays that are formatted like this: Array ( [0] => 10811 [1] => 7527,7556 ) What I would like to do is explode the comma separated values (if they exist) My goal: and then add the exploded value as a new key in the array...like this: Array ( [0] => 10811 [1] => 7527 [2] => 7556 ) I've tried several variants of using explode, but I can't seem to work it out. Any help would be appreciated. Thanks for your consideration. Quote Link to comment https://forums.phpfreaks.com/topic/53091-solved-exploding-comma-separated-in-array/#findComment-262262 Share on other sites More sharing options...
per1os Posted May 26, 2007 Share Posted May 26, 2007 <?php function myExplode($array) { if (is_array($array)) { $i=0; foreach ($array as $key => $val) { if (ereg(",", $val)) { list($val1, $val2) = split(",", $val); $newArray[$i++] = $val1; $newArray[$i++] = $val2; continue; } $newArray[$i++] = $val; } return $newArray; } } ?> Untested but should work. Quote Link to comment https://forums.phpfreaks.com/topic/53091-solved-exploding-comma-separated-in-array/#findComment-262268 Share on other sites More sharing options...
esiason14 Posted May 26, 2007 Author Share Posted May 26, 2007 Thanks, frost110. A couple of tweaks to suit my needs..and it works like a charm...and I learned a lot from it. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/53091-solved-exploding-comma-separated-in-array/#findComment-262313 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.