Jump to content

Insert Into Middle of Array


xProteuSx
 Share

Recommended Posts

HELP!!!  PLEASE!!!

 

Here's what's happening:

 

I have a string much like this one:

 

$str = "84,390961CPK_100,'Pink Coloured Cord',15,'390961CPK_100',1";

 

I need to insert apostrophes around the first instance of 390961CPK_100 so it reads '390961CPK_100' and not simply 390961CPK_100.

 

To do this I have made the string into an array as follows:

 

$str_array = str_split($str);

 

Now I need to insert quotes into the array of characters, then convert the whole mess into back into a string.  I can't go by hard coded index numbers, because all of the text in this string is variable in length, so I have to go by the first and second positions of the commas.

 

I have no idea how to do this, and it needs to be done yesterday!  Mucho Gracias to anyone who can help me with this.

Link to comment
Share on other sites

Thank you so much guys!  I totally forgot about explode and implode (no, I don't work with arrays very often).

 

If anyone is interested, here is what I came up with:

 

$str = "84,390961CPK_100,'Pink Coloured Cord',15,'390961CPK_100',1";
$elements = explode(",", $str);
$elements[1] = "'" . $elements[1] . "'";
$str = implode(",", $elements);
echo $str;

 

The output would result in:

 

84,'390961CPK_100','Pink Coloured Cord',15,'390961CPK_100',1

 

Now I don't have to pull the rest of my hair out!  Thanks soooooooo much!

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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