0

I'm familiar with using in_array when I want to find if a value is in an array.

Is there a similar, and efficient approach, to return true or false if a value is within an array of arrays?

$example = array(
   array( 'small', 6789), 
   array( 'blue', 'duck', 'music'), 
   array( 1000, 'gems' ) 
);

In the above example I would want to return true if I were looking for 'blue'.

I know I can loop through the array and use in_array, but I'm sure I'm missing an easier (and faster) approach.

0

1 Answer 1

2

Unless you want to change the data structure you're using to store this information I don't think there is a better way to check for an individual string than looping through each array and using in_array for each one.

Remember an array is an object in memory. The outer array is storing pointers to each of the arrays inside it, it does not have direct access to their contents.

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

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.