Jump to content

Array key question


Go to solution Solved by Barand,

Recommended Posts

While solving my issue from yesterday, I discovered that I can use an associative array with key names that have space.

ie: $arr['i never knew'][0]

Normally, I would have used underscores to fill spaces OR removed them completely.

Is this a new feature that I stumbled upon? Is it safe to use? Are there drawbacks?

 

Link to comment
https://forums.phpfreaks.com/topic/326665-array-key-question/
Share on other sites

  • Solution

The ability to use strings as array keys (associative keys) has always been a feature of PHP.

The drawbacks are the same as when using string columns as keys in DB tables...

  • they tend to be longer and therefore less efficient
  • great care has to taken to ensure consistency of case, puctuation and spelling
Link to comment
https://forums.phpfreaks.com/topic/326665-array-key-question/#findComment-1648648
Share on other sites

My opinion is that it is not worth whatever benefit you think it will be to use spaces or other extraneous characters as an array key. As Barand was suggesting, you have to be more careful. Otherwise, you create bugs that can be frustrating to find and it can limit you in the future.

 

I recall once where I had a database field with a space in it. I had a function that would query results and return it as an array. No problem, I could reference that value using $dbArray['Field Name']. But then I was rewriting the code as part of refactoring project and instead of a function that returned an array, I instead created a Class and an Object. Now there's a problem. For objects, you normally reference the properties (values) in the format $dbObject->fieldName . That won't work if the property name has a space in it ($dbObject->field Name) as the PHP parsing engine will think the property name ends at the space. You can have property names with a space, but you have to reference them differently. E.g. $object->{'property Name'}. But that format isn't widely used (in my experience) and just makes the code more complicated. It was a big headache to address.

Link to comment
https://forums.phpfreaks.com/topic/326665-array-key-question/#findComment-1648724
Share on other sites

On 1/28/2025 at 1:54 PM, phppup said:

Understood.

I thought it was a little odd, which is why I asked the question.

Are there any restrictions?

Can a key start with a dot/period?

A number? Even a character??

 

The first thing you need to be clear about is what a literal string is vs. an interpolated string.   As this is an important fundamental, I'll assume you know which is which in the following examples.

A key can be any sort of valid PHP string.  So this makes it possible to get yourself into a lot of trouble potentially.

One caveat, as you will see, is that even if you specify a literal string, if PHP determines that string is equivalent to an integer value, it will create an array element indexed by the value.  So you can't have an array key of $r['3'] and $r[3]. 

Also, you should notice that it will cast a float to an integer, and that this behavior is deprecated, so clearly you don't want to ever try and use a float as a key, even though it will actually work (sort of), in that it converts the float to an integer.

 

<?php

$foo = 'bar';
$s = array();

//  Anything that looks like an integer, PHP will convert to a numeric key
$s["0"] = 'Apple';
$s['1'] = 'Banana';
// Notice it casts the flaat to an int and gets 2
$s[2.85] = 22.072;

// However if they are strings, you can key on floating point values
$s['2.85'] = 2.85;
$s["2.86"] = 2.86;

$s['$foo'] = 'literal';

$s["This is $foo"] = 'interpolated';

$s['.foo.bar'] = 'dot foo dot bar';

$s["\u{1CC0}"] = 'unicode';

var_dump($s);

Here's the var_dump result:

Deprecated: Implicit conversion from float 2.85 to int loses precision in php-wasm run script on line 10
array(9) {
  [0]=>
  string(5) "Apple"
  [1]=>
  string(6) "Banana"
  [2]=>
  float(22.072)
  ["2.85"]=>
  float(2.85)
  ["2.86"]=>
  float(2.86)
  ["$foo"]=>
  string(7) "literal"
  ["This is bar"]=>
  string(12) "interpolated"
  [".foo.bar"]=>
  string(15) "dot foo dot bar"
  ["᳀"]=>
  string(7) "unicode"
}

 

Link to comment
https://forums.phpfreaks.com/topic/326665-array-key-question/#findComment-1648735
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.