Vishal Purohit
Vishal Purohit

Reputation: 261

Array values into separate strings

I've an array

Array ( [0] => value1 [1] => value2 [2] => value3 [(n)] => .....)

I want this as following:

  $string1 = 'value1';
  $string2 = 'value2';
  $string3 = 'value3';
  $string(n) = '....';

Please suggest the right way to get this.

Upvotes: 1

Views: 70

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18600

You can use PHP extract() function.

extract($var_array, EXTR_PREFIX_ALL, "string");

Demo

Upvotes: 1

jeremyb
jeremyb

Reputation: 492

Does this work for what you need?

foreach($array as $key => $row) {
    $var = "string" . $key;
    $$var = $row;
}

But you said you need to do a query with the values. Maybe something like this?

$sql = "SELECT * FROM mytable WHERE 1 = 1";
foreach($array as $key => $row) {
    $sql .= " AND string" . $key . " = '" . $row . "'";
}

Upvotes: 0

Related Questions