Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pound key/Hash key/hashtag in php arrays

I am working with Drupal currently and they have this thing with render arrays. They are putting pound keys in front of the configuration indexes of their render arrays which are then used by the render functions.

BUT! the problem has nothing to do with Drupal. The code I am providing in the next couple of lines is completely independent from any php application. Vanilla PHP as some would say.

   <?php
    $array = array(
      '#title' => 'Social media button settings',
      '#type' => 'fieldset',
      array(
        '#title' => 'Facebook',
        '#type' => 'input',
      )
    );

    foreach($array as $i => $d) {
      // This line could contain ANY!!! key as long as it starts
      // with a pound key.
      if (isset($d['#title'])) {
        var_dump('Index: ' . $i);
        echo 'Data:';
        var_dump($d);
        var_dump('$d["#title"]: ' . $d['#title']);
      }
    }

Output is:

string 'Index: #title' (length=13)

Data:

string 'Social media button settings' (length=28)

string '$d["#title"]: S' (length=15)

string 'Index: #type' (length=12)

Data:

string 'fieldset' (length=8)

string '$d["#title"]: f' (length=15)

string 'Index: 0' (length=8)

Data:

array (size=2)
  '#title' => string 'Facebook' (length=8)
  '#type' => string 'input' (length=5)

string '$d["#title"]: Facebook' (length=22)

Expected output would be:

string 'Index: 0' (length=8)

Data:

array (size=2)
  '#title' => string 'Facebook' (length=8)
  '#type' => string 'input' (length=5)

string '$d["#title"]: Facebook' (length=22)

Am I not seeing something here? Is this just something really nasty wrong in PHP?

Would be great to hear from you guys.

func0der

Update ------>>

I am using PHP 5.3.27 wrapped by MAMP. And tried it here: http://writecodeonline.com/php/ Same results both.

like image 237
func0der Avatar asked Jan 29 '26 20:01

func0der


1 Answers

Your code gives me the output you want. I suspect there is something in the environment that messes it up. Or you have made a mistake in copying it or something like that.

-edit-

Conclusion of comments below: isset is buggy in 5.3 and previous. Fixed in 5.4. From php.net:

Non-numeric string offsets - e.g. $a['foo'] where $a is a string - now return false on isset() and true on empty(), and produce a E_WARNING if you try to use them. Offsets of types double, bool and null produce a E_NOTICE. Numeric strings (e.g. $a['2']) still work as before. Note that offsets like '12.3' and '5 foobar' are considered non-numeric and produce a E_WARNING, but are converted to 12 and 5 respectively, for backward compatibility reasons. Note: Following code returns different result. $str='abc'; var_dump(isset($str['x'])); // false for PHP 5.4 or later, but true for 5.3 or less

like image 82
monocell Avatar answered Jan 31 '26 16:01

monocell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!