3

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.

8
  • 2
    This sounds a whole lot like premature optimization. Is this really necessary? Do you have data to prove that it's necessary? Commented Apr 10, 2013 at 19:50
  • 1
    I'm not saying it's necessary but I would like to save space if I can. Commented Apr 10, 2013 at 19:52
  • That's kind of backwards. Why not just store the "key" as a string value in the array and use array_search(). non-presence of the value == boolean not set. Commented Apr 10, 2013 at 19:52
  • 1
    @Michael Berkowski Wouldn't you be iterating over the array as opposed to with the associative array where you wouldn't? Or does iteration happen in both cases? Commented Apr 10, 2013 at 19:57
  • 1
    @Nathan Bouscal I've heard of him, but it's not premature. We got it working, we would just like to make it work better. Commented Apr 10, 2013 at 19:59

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

5 Comments

You are telling that these entries got no keys, which is not true.
Indeed they have numeric keys (or better indices) but this is the way to go with PHP
@WebnetMobile Are you happy now?
That would be good in terms of space but in terms of processor you have to iterate over that collection?
In terms of both, processor and memory I would advice you to use the second suggest using TRUE as value.
1

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

1 Comment

Then the check if the value exists takes O(n) time, compared to key lookup in O(1).
-1

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';

5 Comments

It is NOT possible to have array value without the key. You cannot use own key, but in that case entry will be assigned own unique key
The question asks if you can have a key, without a value, not the other way around. You can certainly have a key that has been assigned a value of null.
I ran a test program and apparently null gets 96 bytes of space
For an array, there is variable storage unit (a zval) allocated for every element. That is simply php overhead required for any variable. If that variable is typed, there will be additional storage overhead appropriate to the variable type. So there's overhead regardless. Also for what it's worth, I agree that 100% that this is micro-optimization that should be avoided, but it doesn't hurt to delve into the internals of php either to help one understand it better.
Furthermore, arrays in php are powerful in terms of their flexibility, but also expensive. Each array element requires a php "bucket" structure, which probably takes the 96 bytes you alluded to, assuming you made an array with a single element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.