Jump to content

Insert Into Middle of Array


xProteuSx

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
https://forums.phpfreaks.com/topic/217049-insert-into-middle-of-array/
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!

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.