1

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.

6
  • 4
    Why do you want to do this rather than simply using the array as it is? Commented Feb 13, 2015 at 11:15
  • If you absolutely have to do this, then extract() with a prefix argument of "string" Commented Feb 13, 2015 at 11:16
  • why? you're searching for extract Commented Feb 13, 2015 at 11:16
  • I need this for set of string to be used in a query as like condition. Commented Feb 13, 2015 at 11:19
  • 2
    No you don't, you can build the query using an array..... there's no need to split it all into separate variables Commented Feb 13, 2015 at 11:20

2 Answers 2

1

You can use PHP extract() function.

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

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

The option needed is EXTR_PREFIX_ALL and you will obtain: $string_0 = 'value1'
0

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 . "'";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.