This is what I am trying to do:
1 - I have an array with a string.
$photographer_array = "'Alberto Korda', 'Annie Leibowitz', 'Ansel Adams'";
2 - I am trying to开发者_如何学Go populate an array with that string.
array($photographer_array);
What it is doing is creating an array with one single entry including all the commas. Why is it reading the commas as a string? Is there anyway I can make it not read the commas as a string and instead use them to separate the values in the array?
Sorry to say that, but you cannot populate an array this way, since strings are usually not parsed and executed. All you can do is:
$photographer_array = array('Alberto Korda', 'Annie Leibowitz', 'Ansel Adams');
Or this (EDIT: Adapted it to your query code):
$photographer_array = array();
while ($photographer_row = $result->fetch_assoc()) {
$photographer_array[] = $photographer_row['photographer_name'];
}
P.S.: You just love long variable names, don't you? ;)
That's because $photographer_array
is a string and using array($photographer_array)
simply puts your string to the array.
Perhaps you want to do:
$photographer_array = explode(',', "'Alberto Korda', 'Annie Leibowitz', 'Ansel Adams'");
This will give you
array(3) {
[0]=>
string(15) "'Alberto Korda'"
[1]=>
string(18) " 'Annie Leibowitz'"
[2]=>
string(14) " 'Ansel Adams'"
}
Or you want:
$photographer_array = array('Alberto Korda', 'Annie Leibowitz', 'Ansel Adams');
// ...
EDIT:
Regarding the code you posted - the following will give you an array of the $photographer_row['photographer_name']
:
// ...
$photographer_array = array();
while($photographer_row = $result->fetch_assoc()) {
$photographer_array[] = $photographer_row['photographer_name'];
}
It's reading the string as a string becuase you enclosed it in quotation marks ( " ). If you want to do it right, try:
$array = array('Alberto Korda', 'Annie Leibowitz', 'Ansel Adams');
Or:
$string = "Alberto Korda, Annie Leibowitz, Ansel Adams";
$arr = explode(',', $string);
you'll notice that the variable $photographer_array
is set to a single string (the value of which is everything between the double quotes). The array function in php accepts a list of objects delimited by commas. While there are commas in your $photographer_array
variable, they are considered to be characters in the string rather than actual delimiters, so you're really just creating an array with only one value which happens to be a string. What you probably want is the following:
$photographer_array = array('Alberto Korda', 'Annie Leibowitz', 'Ansel Adams');
If you've got access to PHP >= 5.3.0, then you can use str_getcsv
, which parses a comma-separated string into an array.
Explode()
is what you need...
http://php.net/manual/en/function.explode.php
Im actually creating an array using values from a query just so you know why I am doing it this way. This is the code I am using to create the string for use in my array.
$sql = "SELECT photographers.photographer_name
FROM photographers
ORDER BY photographer_name ASC
LIMIT 3";
$result = $conn->query($sql) or die(mysqli_error());
$photographer_array = '';
while($photographer_row = $result->fetch_assoc()) {
$photographer_array .= "'" . $photographer_row['photographer_name'] . "', ";
}
$photographer_array = rtrim($photographer_array, ", ");
array($photographer_array);
If you need a string to make a array use:
$photographer_array = "'Alberto Korda', 'Annie Leibowitz', 'Ansel Adams'";
eval("\$myArray = array(" . $photographer_array . ");");
And after use $myArray
精彩评论