I need to make a key name from a array a reference to a different variable.
Like this:
$keys = array('name' => 'abc');
$arr[&$keys['name']] = array();
But it doesn't work:(
is there any solution?
Take off the reference:
$keys=array("name"=>"abc");
$arr=array();
$arr[$keys["name"]]=array();
var_dump($arr);
The output is:
array(1) {
["abc"]=>
array(0) {
}
}
<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
php.net/array !!! there is a text, befor u ask a question , it says that u shoult REALLY search befor u ask , the first ehtry in google , the first ehtry on php.net <- the very first place to look 4 php cuestions
You can't change the key of an array element using a reference, as you want.
You need to create a new one, and unset the previous:
$key = 'abc';
$array[$key] = 'value';
// to change the key:
$new_key = 'def';
$array[$new_key] = $array[$key];
unset($array[$key]);
You wanted something like the code below, but it does not exist nothing like that in PHP:
$key = 'abc';
$array[&$key] = 'value'; // this is not legal in PHP
$key = 'def'; // (this was supposed to change the key)
$keys = array('name' => array('abc' => array()));