0

I have an array holding this data:

Array ( 
  [1402377] => 7 
  [1562441] => 7 
  [1639491] => 9 
  [1256074] => 10 
 )

How can create a string that contains the keys of the above array? Essentially, I need to create a comma separated string that consists of an array's keys

The string would look like: 'key','key','key'

Do I need to create a new array consisting of the keys from an existing array?

The reason I need to do this is because I will be querying a MySQL database using a WHERE in () statement. I would rather not have to query the database using a foreach statement. Am I approaching this problem correctly?

I've tried using a while statement, and I'm able to print the array keys that I need, but I need those keys to be an array in order to send to my model.

The code that allowed me to print the array keys looks like this:

while($element = current($array)) {
            $x = key($array)."\n";
            echo $x;
            next($array);
        }
Ryan
  • 14,392
  • 8
  • 62
  • 102
  • Be careful about [injection](http://unixwiz.net/techtips/sql-injection.html) vulnerabilities. Familiarize yourself with the [array functions](http://it.php.net/manual/en/ref.array.php). – outis Jan 13 '12 at 07:28
  • possible duplicate of [PHP Splitting an Array into two arrays - keys array and values array](http://stackoverflow.com/questions/6234696/), [MySQL Prepared statements with a variable size variable list](http://stackoverflow.com/questions/327274/). – outis Jan 13 '12 at 08:01
  • ...[There is a way to get all keys of the array?](http://stackoverflow.com/questions/3407967/), [Print the keys of an array](http://stackoverflow.com/questions/3507381/), [Parameterizing an SQL IN clause?](http://stackoverflow.com/questions/337704/), [PreparedStatement IN clause alternatives?](http://stackoverflow.com/questions/178479/preparedstatement-in-clause-alternatives) – outis Jan 13 '12 at 08:17

7 Answers7

3
$string = implode(',', array_keys($array));

By the way, for looping over an array consider not using current and next but use foreach:

foreach ($array as $key => $value) {
    //do something
}

This will automatically iterate over the array until all records have been visited (or not at all if there are no records.

hoppa
  • 3,011
  • 18
  • 21
2
$keys = array_keys($array);
$string = implode(' ',$keys);

In your case, were you are using the result in a IN clause you should do: $string = implode(',', $keys);

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
1

$yourString = '';
foreach($yourArr as $key => $val) {
 $yourString .=$key.",";
}
echo rtrim($yourString, ",");

//OR
$yourString = implode(",", array_keys($yourArray));

See : array_keys

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1
implode(', ', array_keys($array));
WoLfulus
  • 1,948
  • 1
  • 14
  • 21
0

Use php array_keys and implode methods

print implode(PHP_EOL, array_keys($element))
Nazariy
  • 6,028
  • 5
  • 37
  • 61
0
The string would look like: 'key','key','key'

$string = '\'' . implode('\',\'', array_keys($array)) . '\'';
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
0

Imploding the arguments and interpolating the result into the query can cause an injection vulnerability. Instead, create a prepared statement by repeating a string of parameter placeholders.

$paramList = '(' . str_repeat('?, ', count($array) - 1) . '?)'
$args = array_keys($array);
$statement = 'SELECT ... WHERE column IN ' . $paramList;
$query = $db->prepare($statement);
$query->execute($args);
outis
  • 75,655
  • 22
  • 151
  • 221