[resolved] more specific in_array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pentascape
Forum Newbie
Posts: 2
Joined: Thu Jul 05, 2007 9:10 am

[resolved] more specific in_array

Post by pentascape »

Hi All,

Is it possible to make in_array a little more specific when it is searching for values (and keys if we're not being strict) in an array?

Lets take the following array ($array) for example:

Code: Select all

Array
(
[0] => apple
[1] => banana
[2] => pear
[3] => Array
(
[one] => apple
[two] => tree
[three] => stuff
)
)
normally

Code: Select all

in_array('apple',$array);
would return true, but how can I specify it only looks in one type of key, for example the 'one' key?

in pseudo code:
is 'apple' in any key named 'one' anywhere in the array
Last edited by pentascape on Thu Jul 05, 2007 10:17 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

'apple'==$array['one']
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Code: Select all

in_array('apple',$array[3]);
???

Oh, missed the last line, nevermind.
Try a foreach loop through the array (and any sub-arrays) and see if it has a key named 'one', and if it does check if it contains 'apple'?
pentascape
Forum Newbie
Posts: 2
Joined: Thu Jul 05, 2007 9:10 am

Post by pentascape »

Yeh, I did a foreach loop in the end, I just wondered if there was a way of doing it within in_array(). Obviously that function is very simple. I was merely trying to avoid a hefty foreach loop.

Thanks though :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

You could use array_keys() with the optional search parameter, but you would still need a loop of some sort for multidimensional arrays, I'd go previous solution though.
Post Reply