我有一个看起来像这样的数组:
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
)
我想要做的是将此数组中的每个值存储到变量中(使用PHP)。例如:
$something1 = "http://www.marleenvanlook.be/admin.php";
$something2 = "http://www.marleenvanlook.be/checklogin.php";
...
答案 0 :(得分:2)
您可以使用extract()
:
$data = array(
'something1',
'something2',
'something3',
);
extract($data, EXTR_PREFIX_ALL, 'var');
echo $var0; //Output something1
的更多信息
答案 1 :(得分:1)
嗯..你可以这样做吗?
$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
这将动态创建名称为$something0
,$something1
等的变量,其中包含foreach
中指定的数组值。
如果您想要涉及密钥,您也可以这样做:
$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
答案 2 :(得分:0)
PHP有一个名为variable variables的东西,它允许你用另一个变量的值命名一个变量。
$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
但问题是为什么你想要这样做? Arrays旨在通过键访问,因此您可以这样做:
echo $something[2];
// http://www.marleenvanlook.be/checkupload.php
答案 3 :(得分:-3)
我要做的是:
$something1 = $the_array[2];
$something2 = $the_array[4];