How to match value in multidimensional 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
crimsontwo
Forum Newbie
Posts: 6
Joined: Fri Apr 16, 2010 3:41 pm

How to match value in multidimensional array?

Post by crimsontwo »

Hi,

I can't seem to use in_array or array_search to match part of a string in my multidimensional array:

$array['cart]['key1'] = "Test string1 of 1"
['key2'] = "Test string2 of 2"
..
I want to search this array for "string2" and get the key associated with this value. Does anyone have a code for that DOES NOT use "foreach" or any other looping through array method?

Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to match value in multidimensional array?

Post by requinix »

If everything to search through is in $array["cart"] then

Code: Select all

array_search("string2", $array["cart"])
crimsontwo
Forum Newbie
Posts: 6
Joined: Fri Apr 16, 2010 3:41 pm

Re: How to match value in multidimensional array?

Post by crimsontwo »

doesn't work because everything is in $array["cart"][???]
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to match value in multidimensional array?

Post by requinix »

Oh, part of a string. Missed that.

Then no. There is no built-in function to do that. You'll have to write some code somewhere to do it: either a function or a loop.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: How to match value in multidimensional array?

Post by Weiry »

crimsontwo wrote: I want to search this array for "string2" and get the key associated with this value.
Pretty easy. foreach() loop using if(strpbrk())
crimsontwo wrote:Does anyone have a code for that DOES NOT use "foreach" or any other looping through array method?
Illogical statements are impossible... even in_array or search_array loop through the array.
Post Reply