如何在php中设置动态数组名称

发布于 2024-12-09 06:21:36 字数 627 浏览 1 评论 0原文

您好,我正在尝试使用动态数组名称。但是当我运行此代码时,我收到错误 $marker is undefined

 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++;
                  }

           }

我如何创建动态数组名称???

hi i am trying to use dynamic array name . but when i run this code i get the error $marker is undefined .

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷心人i 2024-12-16 06:21:36

阅读精美手册。 PHP 中的点运算符与 Javascript 和类似语言中的点运算符完全无关 - 它执行字符串连接。我不太明白你想要做什么,但我相当确定字符串连接不是它。

澄清一下,这是做什么的:

$marker.$key ['draggable'] = 'TRUE';

...是这个;

  • 获取$marker中的值,将其解释为字符串
  • 字符串
  • 获取$key中的值,将其解释为连接$marker和<的 code>$string
  • 将结果字符串解释为数组,并将“draggable”处的元素设置为字符串 (!)“TRUE”。我什至不确定它的作用 - 字符串确实允许数组样式索引(引用单个字符),但我不知道非整数索引会产生什么。

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.

To clarify, what this does:

$marker.$key ['draggable'] = 'TRUE';

...is this;

  • get the value in $marker, interpret it as a string
  • get the value in $key, interpret it as a string
  • concatenate $marker and $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.
岁月打碎记忆 2024-12-16 06:21:36

试试这个,当你进行串联时,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++;
                  }
           }

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

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++;
                  }
           }
木森分化 2024-12-16 06:21:36

Php中的动态数组名称可以这样完成

foreach($arr as $key => $value) {
$myarray[$key] = $value;
}

但是,您也可以这样做,设置数组变量

foreach($arr as $key => $value) {
${$key}[$key] = $value;
}

Dynamic array names in Php can be done like this

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文