0

Let's say I've uploaded this string into a database

"Red", "Blue", "Orange", "Green"

Now I've set it into a variable which I call strColorArray:

$strColorArray = '"Red", "Blue", "Orange", "Green"';

I can't seem to get it to work the way I want it to when I put it in an array, I wondering if there is an easy to understand way to fix this.

$strColorArray = '"Red", "Blue", "Orange", "Green"';
$colorArray =  array($strColorArray);
echo $colorArray[0];

Currently

echo $colorArray[0]; gives "Red", "Blue", "Orange", "Green"

I would like it to have $colorArray[0] as Red, $colorArray[1] as Blue, and so on.

5 Answers 5

3

You need to use the explode() function to convert a string to array. In your case, you might need to do something more:

$strColorArray = '"Red", "Blue", "Orange", "Green"';
$strColorArray = str_replace(array('"', ', '), array("", ","), $strColorArray); // Remove the spaces and double quotes
$colorArray = explode(",", $strColorArray);

When I give var_dump($colorArray) (to check the contents of a variable), I get this output:

array(4) {
  [0]=>
  string(3) "Red"
  [1]=>
  string(4) "Blue"
  [2]=>
  string(6) "Orange"
  [3]=>
  string(5) "Green"
}

Output: http://ideone.com/3MhGle

Sign up to request clarification or add additional context in comments.

Comments

0
$strColorArray = '"Red", "Blue", "Orange", "Green"';
$stripper =  str_replace(array("'",'"'),'',$strColorArray);
$colorArray = explode(',',$stripper);
echo $colorArray[0];

2 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations!
@FrankerZ Thanks for your suggestion.
0

"One-line" solution using preg_match_all function:

preg_match_all("/\w+/i", $strColorArray, $matches);

print_r($matches[0]);

The output:

Array
(
    [0] => Red
    [1] => Blue
    [2] => Orange
    [3] => Green
)

http://php.net/manual/en/function.preg-match-all.php

3 Comments

@PraveenKumar, you have confused, it's not JavaScript regex
Oh, so JavaScript only has g, is it? :P
@PraveenKumar, I wouldn't say that ONLY javascript regexpes have g modifier, maybe other languages have it too. But, there's no g modifier for PHP regexp functions
0

Here's another approach that doesn't use regexes:

$out = array_map(
   function($item) {
      return trim($item, '" ');
   },
   explode(',', $in)
);

Comments

-1

So long as you are in control of the data going in and coming out of the database, then this is a simple way given that format:

eval("\$colorArray = [$strColorArray];");

Or for older PHP versions:

eval("\$colorArray = array($strColorArray);");

1 Comment

It works for the sample, but still a pretty bad suggestion. Even if you 'control' the data, it doesn't mean that the real data will not contain $ or one of the many other characters that breaks this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.