0

I have db array :

Array
(
    [28] => Array
        (
            [0] => child
            [1] => baby
            [2] => new d
            [3] => christmas
        )

    [29] => Array
        (
            [0] => christmas
        )

    [30] => Array
        (
            [0] => business
            [1] => stock
        )

    [32] => Array
        (
            [0] => apparel
            [1] =>  clothing
            [2] =>  contemporary apparel
        )

    [49] => Array
        (
            [0] => car rental
            [1] => car rent
            [2] => rent car
            [3] => rent a car
            [4] => car rentals
            [5] => car1 rentals1
        )
)

I have another array Search This array values in above array array ("christmas","apparel");

So I want the result : 28,29,32 (This is key of array)

1
  • have you tried anything? Commented Dec 5, 2014 at 11:30

2 Answers 2

4

Try this -

$b = array("christmas","apparel");
$keys = array();
foreach ($dbArray as $key => $values) {
   $check = array_intersect($values, $b);
   if (!empty($check)) {
       $keys[] = $key;
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

If dbArray is too big means 10000 Records then i looping then will it takes too much time or not?
You should take care if there is very large number of records.I dont think (not sure) there would be any other solution to do this with php, rather you can do this with your query.
0

Below is a recursive function to search a value in multidimensional array.

function search_in_array($srchvalue, $array)
    {
        if (is_array($array) && count($array) > 0)
        {
            $foundkey = array_search($srchvalue, $array);
            if ($foundkey === FALSE)
            {
                foreach ($array as $key => $value)
                {
                    if (is_array($value) && count($value) > 0)
                    {
                        $foundkey = search_in_array($srchvalue, $value);
                        if ($foundkey != FALSE)
                            return $foundkey;
                    }
                }
            }
            else
                return $foundkey;
        }
    }

It will return you the key where the value is found. Don't forget to accept answer if it helps you

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.