Search code examples
phparrayscodeigniterdynamic-data

how to set dynamic arrray names in php


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


Solution

  • 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.