Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Nothing is wrong, your expectation simply disagrees with how PHP works.

If you want the "title" and "type" values to be treated as pairs you need to somehow group them together (not just "put them next to each other"). One way to do that would be to wrap another array around each pair, which would result in your expected output. You are already doing this for the Facebook/input pair, so removing the rest of the data gives the output you expect:

$array = array(
  array(
    '#title' => 'Facebook',
    '#type' => 'input',
  )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top