hi i am trying to use dynamic array name . but when i run this code i get the error $marker is undefined
.
嗨,我正在尝试使用动态数组名称。但是,当我运行此代码时,我得到错误$ marker未定义。
if (isset($arr)) {
foreach ($arr as $key => $value) {
$marker.$key = array();
$marker.$key ['position'] = $value['lat'] . ',' . $value['long'];
$marker.$key ['draggable'] = 'TRUE';
$marker.$key ['ondragend'] = "test(this.getPosition().lat(),this.getPosition().lng())";
$this->ci->googlemaps->add_marker($marker.$key);
$i++;
}
}
how can i create dynamic array name ????
我该如何创建动态数组名称????
3 个解决方案
#1
2
Read The Fine Manual. The dot operator in PHP is completely unrelated to the dot operator in Javascript and similar languages - it does string concatenation. I don't quite understand what it is you're trying to do, but I'm fairly sure string concatenation is not it.
阅读精细手册。 PHP中的点运算符与Javascript和类似语言中的点运算符完全无关 - 它执行字符串连接。我不太明白你想要做什么,但我相当确定字符串连接不是它。
To clarify, what this does:
澄清一下,这是做什么的:
$marker.$key ['draggable'] = 'TRUE';
...is this;
...这是;
- get the value in
$marker
, interpret it as a string - 获取$ marker中的值,将其解释为字符串
- get the value in
$key
, interpret it as a string - 获取$ key中的值,将其解释为字符串
- concatenate
$marker
and$string
- 连接$ marker和$ string
- interpret the resulting string as an array, and set the element at 'draggable' to the string (!) 'TRUE'. I'm not even sure what this does - string do allow for array-style indexing (which references individual characters), but I have no idea what non-integer indexes would yield.
- 将结果字符串解释为数组,并将'draggable'元素设置为字符串(!)'TRUE'。我甚至不确定这是做什么的 - 字符串允许数组样式索引(引用单个字符),但我不知道非整数索引会产生什么。
#2
1
Try this, when you do your concatenation PHP sees only $key to be an array, and concatenates the wrong way. Anyway, where is $marker defined??
试试这个,当你进行连接时,PHP只看到$ key是一个数组,并以错误的方式连接。无论如何,$ marker定义在哪里?
if (isset($arr)) {
foreach ($arr as $key => $value) {
$myarray = $marker.$key;
$myarray = array();
$myarray['position'] = $value['lat'] . ',' . $value['long'];
$myarray['draggable'] = 'TRUE';
$myarray['ondragend'] = "test(this.getPosition().lat(),this.getPosition().lng())";
$this->ci->googlemaps->add_marker($myarray);
$i++;
}
}
#3
0
Dynamic array names in Php can be done like this
Php中的动态数组名称可以像这样完成
foreach($arr as $key => $value) {
$myarray[$key] = $value;
}
Yet, you can also do like this, to set the array-variable
然而,你也可以这样做,设置数组变量
foreach($arr as $key => $value) {
${$key}[$key] = $value;
}
#1
2
Read The Fine Manual. The dot operator in PHP is completely unrelated to the dot operator in Javascript and similar languages - it does string concatenation. I don't quite understand what it is you're trying to do, but I'm fairly sure string concatenation is not it.
阅读精细手册。 PHP中的点运算符与Javascript和类似语言中的点运算符完全无关 - 它执行字符串连接。我不太明白你想要做什么,但我相当确定字符串连接不是它。
To clarify, what this does:
澄清一下,这是做什么的:
$marker.$key ['draggable'] = 'TRUE';
...is this;
...这是;
- get the value in
$marker
, interpret it as a string - 获取$ marker中的值,将其解释为字符串
- get the value in
$key
, interpret it as a string - 获取$ key中的值,将其解释为字符串
- concatenate
$marker
and$string
- 连接$ marker和$ string
- interpret the resulting string as an array, and set the element at 'draggable' to the string (!) 'TRUE'. I'm not even sure what this does - string do allow for array-style indexing (which references individual characters), but I have no idea what non-integer indexes would yield.
- 将结果字符串解释为数组,并将'draggable'元素设置为字符串(!)'TRUE'。我甚至不确定这是做什么的 - 字符串允许数组样式索引(引用单个字符),但我不知道非整数索引会产生什么。
#2
1
Try this, when you do your concatenation PHP sees only $key to be an array, and concatenates the wrong way. Anyway, where is $marker defined??
试试这个,当你进行连接时,PHP只看到$ key是一个数组,并以错误的方式连接。无论如何,$ marker定义在哪里?
if (isset($arr)) {
foreach ($arr as $key => $value) {
$myarray = $marker.$key;
$myarray = array();
$myarray['position'] = $value['lat'] . ',' . $value['long'];
$myarray['draggable'] = 'TRUE';
$myarray['ondragend'] = "test(this.getPosition().lat(),this.getPosition().lng())";
$this->ci->googlemaps->add_marker($myarray);
$i++;
}
}
#3
0
Dynamic array names in Php can be done like this
Php中的动态数组名称可以像这样完成
foreach($arr as $key => $value) {
$myarray[$key] = $value;
}
Yet, you can also do like this, to set the array-variable
然而,你也可以这样做,设置数组变量
foreach($arr as $key => $value) {
${$key}[$key] = $value;
}