Jump to content

[SOLVED] exploding comma separated in array


esiason14

Recommended Posts

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 )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.