1

I have the following array

$a = ["one", "dos" => "two", "three"];

As you see the second element has the key for his value set explicitely, but the other 2 items do not.

I want to loop through the array, but do something different, depending if the key for that item was set explicitly or not. Kinda like this:

foreach($a as $value){
    if( has_explicit_key($value) )
        // Do something
    else
        // Do other stuff
}

How can I achieve this?

PS: I guess I could check if the key is an integer, but if the key is set explicitly as an integer, that would not work, right?

5
  • 1
    I'm sorry, but I really don't understand what do you mean by "set explicitly" Can you explain? Commented Dec 10, 2014 at 15:38
  • Yes and no... it depends... the automatic keys will always be integers. Is it possible for one of your explicitly set keys to also be an integer? If not you could use that to know if it's an automatically set key or not. Commented Dec 10, 2014 at 15:39
  • 1
    There is no way of telling if an integer key is autogenerated or deliberately created by the coder. Commented Dec 10, 2014 at 15:46
  • I guess that is the correct answer... Commented Dec 10, 2014 at 15:48
  • In code, create another array of just the explicit keys and test against that. Commented Dec 10, 2014 at 15:52

3 Answers 3

2

try this

foreach($a as $key=>$value){
    if( is_int($key) )
        // Do something
    else
        // Do other stuff
}

this is the closest approach since keys are usually, 0,1,2......

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

3 Comments

Can keys be set as integers explicitely? If the answer is yes, this approach would not work...
@Dbugger yes, they could be set as integer
you usually access an array item by its key value as integer e.g $array[0], so i believe in this case it's what you need since all the associative keys assigned are "one" , "two" as string
1

In your specific case, you can exploit the fact that elements without explicit string keys automatically receive integer indexes:

$a = ["one", "dos" => "two", "three"];

foreach ($a as $k => $v) {
    if (is_int($k)) {
        // Do something
    } else {
        // Do other stuff
    }
}

If you allow for the explicit keys to be scalars other than strings (integer, float, boolean, etc), then there is no way (at run-time) to distinguish between non-string keys supplied by the user and integer keys filled in by the parser. Specifically, refer to the PHP source function zend_ast_add_array_element. In that function, when the key is not explicitly given (offset IS_UNDEF), then PHP assigns one with zend_hash_next_index_insert and records no bookkeeping note of that fact.

Now, if you don't mind, and are capable of statically analyzing the data structure, just tokenize or parse the PHP code and see if T_DOUBLE_ARROW precedes the array value. This is probably not worth the effort and only works on static code.

4 Comments

Can keys be set as integers explicitely? If the answer is yes, this approach would not work...
@Dbugger: Yes they can, which is why I said "in your specific case". :) Are you seeking a general solution?
Yes, a general solution would be best
@Dbugger: I elaborated on why a general solution is not possible based on how the PHP parser currently works.
0

You can loop through the array using

foreach($a as $key => $value) {
    /* stuff */
}

To check if the key has been set explicitly can probably only be done by checking if the key is numerical (PHP will assign numerical keys to values without any keys in arrays). Of course this means that you won't be able to detect a key that was set explicitly and is numeric.

So unless there's some function (which I'm unaware of) this would be the only way.

Comments

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.