Jump to content

[SOLVED] exploding comma separated in array


esiason14

Recommended Posts

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.

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.