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?
    – gen_Eric
    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.
    – the_pete
    Sep 12, 2014 at 19:34
  • 5
    You're clearly solving a wrong problem
    – Yang
    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

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
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
-3

What I would do is:

$something1 = $the_array[2];
$something2 = $the_array[4];
3
  • 1
    And you would do that manually every-time a new key is added?
    – Yang
    Sep 12, 2014 at 19:41
  • he asked "What I want to do is store each value from this array to a variable" - the example above is very basic.
    – unixmiah
    Sep 12, 2014 at 19:56
  • if he wants to read though the array with a foreach loop, he can accomplish variable naming the same way also
    – unixmiah
    Sep 12, 2014 at 19:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.