tags:

views:

100

answers:

8

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 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?

A: 

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.

Dominic Rodger
`str_getcsv()` is a PHP >= 5.3.0 function. Just to take note of this.
Stefan Gehrig
Thanks Stefan, edited in.
Dominic Rodger
A: 

Explode() is what you need...

http://php.net/manual/en/function.explode.php

rikh
That's going to end in pain if the values themselves contain commas.
Dominic Rodger
Also not the right thing for what Chris really wants, although you couldn't know that obviously...
Franz
+3  A: 

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'];
}
Stefan Gehrig
Yeah, that's exactly what I needed. Franz beat you to it. Thanks for your help!
zeckdude
+1  A: 

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);
zeckdude
In that case just use my second approach.
Franz
That doesn't change anything. `$photographer_array` is a string, period. Why don't you push values into array instead of concatenating them into string?
n0rd
Y'know, PHP is not strong-typed, but that doesn't mean it does on the fly conversion between strings that look like written out arrays and arrays.
Boldewyn
nOrd, that's what Franz's second approach does and I have applied it now.while($photographer_row = $result->fetch_assoc()) {$photographer_array[] = $photographer_row['photographer_name'];}
zeckdude
+6  A: 

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? ;)

Franz
Thank you very much Franz! That worked great! Sorry to confuse everyone. I should have showed you my query right away. Thanks for all the quick replies!
zeckdude
Yeah im running multiple queries on that page, so I wanted it to be obvious which one is which, therefore the long variable names. weird habit i guess
zeckdude
+1  A: 

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);

TheGrandWazoo
+1  A: 

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');
Jurassic_C
+1  A: 

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

Cesar