Page 1 of 1

How to match value in multidimensional array?

Posted: Mon Apr 19, 2010 5:53 pm
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!

Re: How to match value in multidimensional array?

Posted: Mon Apr 19, 2010 6:31 pm
by requinix
If everything to search through is in $array["cart"] then

Code: Select all

array_search("string2", $array["cart"])

Re: How to match value in multidimensional array?

Posted: Mon Apr 19, 2010 7:28 pm
by crimsontwo
doesn't work because everything is in $array["cart"][???]

Re: How to match value in multidimensional array?

Posted: Mon Apr 19, 2010 8:33 pm
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.

Re: How to match value in multidimensional array?

Posted: Tue Apr 20, 2010 3:58 am
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.