0

I have an array that looks something like this:

Array
(
    [2] => http://www.marleenvanlook.be/admin.php
    [4] => http://www.marleenvanlook.be/checklogin.php
    [5] => http://www.marleenvanlook.be/checkupload.php
    [6] => http://www.marleenvanlook.be/contact.php
)

What I want to do is store each value from this array to a variable (using PHP). So for example:

$something1 = "http://www.marleenvanlook.be/admin.php";
$something2 = "http://www.marleenvanlook.be/checklogin.php";
...
3
  • 8
    Why do you want to do that? Commented Sep 12, 2014 at 19:33
  • 2
    You can access the array elements directly using $arr[#] so, for instances echo $arr[2]; would output http://www.marleenvanlook.be/admin.php. This is easier than setting up a variable for each not to mention SLIGHTLY more efficient as you won't be setting up more variables than needed. Commented Sep 12, 2014 at 19:34
  • 5
    You're clearly solving a wrong problem Commented Sep 12, 2014 at 19:39

4 Answers 4

2

You can use extract():

$data = array(
    'something1',
    'something2',
    'something3',
);
extract($data, EXTR_PREFIX_ALL, 'var');
echo $var0; //Output something1

More info on https://www.php.net/manual/en/function.extract.php

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

Comments

2

Well.. you could do something like this?

$myArray = array("http://www.marleenvanlook.be/admin.php","http://www.marleenvanlook.be/checklogin.php","etc");

$i = 0;
foreach($myArray as $value){
    ${'something'.$i} = $value;
    $i++;
    }

echo $something0; //http://www.marleenvanlook.be/admin.php

This would dynamically create variables with names like $something0, $something1, etc holding a value of the array assigned in the foreach.

If you want the keys to be involved you can also do this:

$myArray = array(1 => "http://www.marleenvanlook.be/admin.php","http://www.marleenvanlook.be/checklogin.php","etc");

foreach($myArray as $key => $value){
    ${'something'.$key} = $value;
    }

echo $something1; //http://www.marleenvanlook.be/admin.php

Comments

1

PHP has something called variable variables which lets you name a variable with the value of another variable.

$something = array(
    'http://www.marleenvanlook.be/admin.php',
    'http://www.marleenvanlook.be/checklogin.php',
    'http://www.marleenvanlook.be/checkupload.php',
    'http://www.marleenvanlook.be/contact.php',
);

foreach($something as $key => $value) {
    $key = 'something' . $key;
    $$key = $value;

    // OR (condensed version)
    // ${"something{$key}"} = $value;
}

echo $something2;
// http://www.marleenvanlook.be/checkupload.php

But the question is why would you want to do this? Arrays are meant to be accessed by keys, so you can just do:

echo $something[2];
// http://www.marleenvanlook.be/checkupload.php

Comments

-3

What I would do is:

$something1 = $the_array[2];
$something2 = $the_array[4];

3 Comments

And you would do that manually every-time a new key is added?
he asked "What I want to do is store each value from this array to a variable" - the example above is very basic.
if he wants to read though the array with a foreach loop, he can accomplish variable naming the same way also

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.