0

I want to assign the values of an array to a string with a variable:

    $my_array_values = array("apple", "orange", "cellular", "box");

//my function
function my_function ($my_array_values, $string) {
foreach ($my_array_values as $my_array_value) {
$string_query = $string;
 echo $string_query . "<br>";
}
}

//string to loop the variable inside the foreach
$string_variable = "SELECT login FROM table WHERE login = '" . $my_array_value . "'";
my_function ($my_array_values, $string_variable);

If I echo it, the result is this (not taking the array values of the variable):

    SELECT login FROM table WHERE login = ''
SELECT login FROM table WHERE login = ''
SELECT login FROM table WHERE login = ''
SELECT login FROM table WHERE login = ''

The result should be like this:

    SELECT login FROM table WHERE login = 'apple'
SELECT login FROM table WHERE login = 'orange'
SELECT login FROM table WHERE login = 'cellular'
SELECT login FROM table WHERE login = 'box'

How I can do that? Kind regards

1
  • And if the $string_variable is like this?: $string_variable = "SELECT login FROM table WHERE login = '" . $my_array_value . "' AND STATUS = 'Active' AND e_mail IS NOT NULL AND e_mail != '' AND usertype != 'home' AND usertype != 'box'"; Commented May 18, 2018 at 17:16

3 Answers 3

1

What is it you are trying to achieve? The MySQL IN function looks for values "IN" a subset or other pool of info.

$query= "SELECT login FROM table WHERE login IN ({implode(',', $my_array_values)})";

Would this be easier than looping over and over?

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

3 Comments

I think what I mean is, why are you iterating an SQL statement over and over? Also, be mindful of SQL injection from within your array.
Yes I know, the SQL is just a string example concatenated with a variable.
Ahh I see. My apologies, the example confused me. In that case, either of the answers below are spot on for what you need :)
0

The correct way is like this:

$my_array_values = array("apple", "orange", "cellular", "box");

//my function
function my_function ($my_array_values, $string) {
  foreach ($my_array_values as $my_array_value) {
    $string_query = $string . $my_array_value . "'";
     echo $string_query . "<br>";
  }
}

//string to loop the variable inside the foreach
$string_variable = "SELECT login FROM table WHERE login = '" ;
my_function ($my_array_values, $string_variable);

2 Comments

And if the $string_variable is like this?: $string_variable = "SELECT login FROM table WHERE login = '" . $my_array_value . "' AND STATUS = 'Active' AND e_mail IS NOT NULL AND e_mail != '' AND usertype != 'home' AND usertype != 'box'";
You would do it the same way. You can concatenate any way you want. Just remember if you want each value in the array, you have to do it like in my answer.
0

No need to create extra variables in your function. Simply echo what was passed along with the value extracted from the array that was passed.

$my_array_values = array("apple", "orange", "cellular", "box");

//my function
function my_function ($my_array_values, $string) {

    foreach ($my_array_values as $my_array_value) {
        echo $string . $my_array_value . "'<br>";
    }

}  // end of my_function

//string to loop the variable inside the foreach
$string_variable = "SELECT login FROM table WHERE login = '";
my_function ($my_array_values, $string_variable);

4 Comments

And if the $string_variable is like this?: $string_variable = "SELECT login FROM table WHERE login = '" . $my_array_value . "' AND STATUS = 'Active' AND e_mail IS NOT NULL AND e_mail != '' AND usertype != 'home' AND usertype != 'box'";
What if it is @Max? The question was pretty specific and did not include the @my_array_value variable in the middle of the query string. There are a lot of ways to break things with the answers given.
I know @Dave: my specific question is: without breaking the string, how I can loop this variable $my_array_value concatenated inside the string?
You seem to have changed your question. Perhaps we need to step back a little and figure out exactly what it is you are trying to accomplish.

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.