0

I have this array of values:

$col_names = array(
                   'Call For?' => 'assigned_to', 
                   'Message' => 'message', 
                   'Name' => 'caller_name', 
                   'Company Name' => 'caller_company', 
                   'Telephone' => 'caller_telephone'
                  );

I then use an in_array function to check if a value is in the above array:

if(in_array(trim(tdrows($node->childNodes)), $col_names)) {

I want to check the first value in the array, for example check Call For? rather than assigned_to

how can i do this?

3 Answers 3

1

Or better yet use array_key_exists(). This is much more efficient then retrieving the entire list of keys and doing an in_array lookup as suggested in sgt's answer:

if(array_key_exists(trim(tdrows($node->childNodes)), $col_names)) {
Sign up to request clarification or add additional context in comments.

Comments

0

you can you array_flip() which will flip the array.

in_array(trim(tdrows($node->childNodes)), array_flip($col_names))

or if you dont need the values then try with array_keys() which will extract the keys only

in_array(trim(tdrows($node->childNodes)), array_keys($col_names))

2 Comments

Not the most efficient way to do it. If he only needs to check the existence of the key it's better to use array_key_exists()
yup.. it will be the most efficient. missed this. :) will give you +1..@AlexLinte
0

You can use the array_key_exists

to find if key exists or not. This is better than the in_array method

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.