user528451
user528451

Reputation: 133

PHP associative arrays keys only

Is it possible to declare an array element key and not define it a value (like non-array variables)? This way if you have an associative array of booleans, you need only check if the key exists rather than assigning a boolean value. But you'd still have the advantage of not having to iterate over the array when checking if a key exists.

This would be a space saving measure. It appears 'null' gets allocated space.

Upvotes: 3

Views: 6228

Answers (4)

gview
gview

Reputation: 15361

Yes it's possible. You can also use array_key_exists to check for those values. PHP seperates the hash map of variable names from the actual storage of data (google on zval if you're interested). With that said, arrays pay an additional penalty in having to also have an associated "bucket" structure for each element, that depending on your os and compile options can be as large as 96 bytes/per. Zvals are also as much as 48 bytes each, btw.

I don't think there's any chance you're going to get much value from this scheme however, but purely from a hypothetical standpoint, you can store a null value.

<?php

$foo = array('a' => null, 'b' => null);

if (array_key_exists('a', $foo))
    echo 'a';

This does not save you any memory however, if compared to initializing to a boolean. Which would then let you do an isset which is faster than making the function call to array_key_exists.

<?php
$foo = array('a' => true, 'b' => true);

if (isset($foo['a']))
   echo 'a';

Upvotes: -1

Whistletoe
Whistletoe

Reputation: 573

If i understand correctly. You plan to use an associative array like this:

key      value
"bool1"  ""
"bool2"  ""
"bool3"  ""

And if a key exists, then the bool is "true".

Why not just use an ordinary array like this?:

key   value
1     "bool1"
2     "bool2"
3     "bool3"

If the value exists, then the bool is "true".

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 158020

If you don't want to have a dictionary structure like in an accoc array, then you just want a set of values, like this:

$array = ('red', 'green', 'blue');

To check if a key (item) exists just use in_array():

if(in_array('red', $array)) {
   // -> found
}

However,you should note that php will internally create numeric indicies in this case.


Another way to go would be to assign TRUE to all values. This would at least take less memory. Like this

$array (
    'red' => TRUE,
    'green' => TRUE,
    'blue' => TRUE
);

and check existence using isset() Like:

if(isset($array['red'])) {
    // -> found
}

Note: I wouldn't advice you to use NULL as the value. This because you cannot use isset() in this case as isset will return false if the value of a key is NULL. You'll have to use array_key_exists() in this case what is significantly slower than isset().


Conclusion: In terms of processor and memory consumption I would suggest the second advice in PHP. The memory consumption should be the same as with numeric arrays but search operations are optimized.

Upvotes: 2

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

No. Array element always have key and value, however you may just put anything as your value if you do not care (i.e. empty string). In your case you should just add these keys to your array which are of value i.e. true. And then when you will be looking for it and will be unable to find you can assume it's false. But in general you are doing things wrong. You are NOT really saving here but make your code unclean and hard to read and maintain. Do not do this

Upvotes: 2

Related Questions