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)

Yatin Mistry
  • 1,246
  • 2
  • 13
  • 35

2 Answers2

4

Try this -

$b = array("christmas","apparel");
$keys = array();
foreach ($dbArray as $key => $values) {
   $check = array_intersect($values, $b);
   if (!empty($check)) {
       $keys[] = $key;
   }
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • If dbArray is too big means 10000 Records then i looping then will it takes too much time or not? – Yatin Mistry Dec 05 '14 at 11:49
  • 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. – Sougata Bose Dec 05 '14 at 11:53
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

Veerendra
  • 2,562
  • 2
  • 22
  • 39