1

Lets say I have an array looking like this:

$sql = array("name"=>"Peter", "active"=>1 , "age"=>30)

and a loop looking like this:

    for($i=0;$i<count($sql);$i++){
       $value[$i] = ($sql[$i]);
       echo $value[$i];
     }

I want the loop to iterate through the array and assign each value to a new variable. In this code i tried to make it store the values in:

value1

value2

value3

But sadly this doesnt work, thus I am here seeking help. Or is it a problem that i got an associative array instead of a numeric one? I dont want to use this loop on this array only but on other arrays with different keys and length aswell.

Edit: I think I may have not wrote it cleary enough to tell you what i want to achieve: I want to have three string values at the end of the loop not stored in an array:

Variable1 should contain "Peter"

Variable2 should contain "1"

Variable3 should contain "30"

Plus I want this loop to be dynamic, not only accepting this specific array but if I were to give it an array with 100 Values, I would want to have 100 different variables in which the values are stored.

Sorry for not being clear enough, I am still new at stackoverflow.

7
  • $value1 Should contain value of $sql[1] is it? Commented Oct 17, 2019 at 7:32
  • Yes, or rather the first value of the array in this example "Peter" Commented Oct 17, 2019 at 7:33
  • Or is it a problem that i got an associative array instead of a numeric one? Yes, that is the problem. You have got keys and you can't access a location with a numeric index. You will have to give them keys like echo $sql['name']; Commented Oct 17, 2019 at 7:40
  • 1
    I hope you don't mind me asking, but why do you want to do this? Arrays are a good fit for data, and extraction to more than a handful of variables can make it unweildy. What are you planning to do with these variables? Commented Oct 17, 2019 at 8:25
  • I plan to use these variables in various sql queries(insert/delete/update), which require strings values to work accordingly as far as i know. Commented Oct 17, 2019 at 8:43

2 Answers 2

1

Going by your condition, assign each value to a new variable, I think what you want would be to use Variable variables. Here is an example:

<?php
    $sql = array("name"=>"Peter", "active"=>1 , "age"=>30);
    $count = 1;
    foreach ($sql as $value) {
        $x = 'value'.$count;
        $$x = $value; //here's the usage of Variable variables
        $count++;
    }

    echo $value1.'<br/>';
    echo $value2.'<br/>';
    echo $value3.'<br/>';

I went to your sample variables ($value1, $value2, etc.). I also changed your loop to foreach to easily loop the array. And I also added a $count that will serve as the number of the $value variable.

The $count wouldn't be necessary if your index are numeric, but since its an associative array, something like this is needed to differentiate the variables created

A brief explanation as requested:

$x contains the name of the variable you want to create (in this case, value1), then when you add another $ to $x (which becomes $$x), you are assigning value to the current value of $x (this equals to $value1='Peter')

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

2 Comments

Thank you. This worked just perfectly. If you dont mind, could you explain me in detail what exactly the variable variable is for in this example?
@Tenka Added a short explanation, just comment if you want more clarifications
0

To dynamically define a variable use $$. Demo

$sql = array("name"=>"Peter", "active"=>1 , "age"=>30);
$index = 1;
foreach($sql as $value){
    ${"value" . $index++} = $value;
}

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.