Even if I had an error when I tried to reference the key
$a = array('email'=>'orange@test','topic'=>'welcome onboard','timestamp'=>'2017-10-6');
$b = array();
foreach($a as &$key=>&$v){
$b[] = &$v;
}
Fatal error: key element cannot be a link
Can someone explain to me why you cannot pass the key as a reference?
Because the language does not support this. It will be difficult for you to find this ability in most languages, hence the key.
So am I stuck with something like this?
Yes. The best way is to create a new array with the appropriate keys.
Any alternatives?
- . , , SQL.
Re:
:
<?php
$a = array('email'=>'orange@test','topic'=>'welcome onboard','timestamp'=>'2017-10-6');
$b = array();
foreach($a as $key=>&$v){
$b[] = &$v;
}
echo "<pre>";
print_r($a);
echo "</pre>";echo "<pre>";
print_r($b);
Array
(
[email] => orange@test
[topic] => welcome onboard
[timestamp] => 2017-10-6
)
Array
(
[0] => orange@test
[1] => welcome onboard
[2] => 2017-10-6
)