The array will not produce data if I use a variable inside the array

advertisements

Why does this not work?

    $w = '"one","two"';
    $a = array($w);
    for($i = 0; $i < count($a); $i++) {
        echo $a[$i].'<br />';
        }

The one above outputes: "one","two"

But This does?

    $a = array("one","two");
    for($i = 0; $i < count($a); $i++) {
        echo $a[$i].'<br />';
        }

The one above outputs:

one
two

This has to be dynamically pulled from a database. I'm storing the info as an array with quotes around each element. So, when I want to pull the data I'm just going to throw a variable for that row in an array. But, since that isn't working how do I make it work? Thank you


$w = '"one","two"';
$a = array($w);

Creates an array with one element "one","two" (check with var_dump($a);)

$a = array("one","two");

Creates an array with two elements "one" and "two"

If the data comes from the database as a string of comma-separated items, you could split them with explode(), but it is a terrible practice - you shouldn't store multiple values in a string.