-2

I have the following array:

Array
(
    [0] => Array
        (
            [6] => 2015-02-27 19:00
        )

    [1] => Array
        (
            [6] => 2015-02-27 20:00
        )

    [2] => Array
        (
            [6] => 2015-02-27 21:00
        )

)

The "6" is a category and I want to check if the category exists with the according date.

I have

$category = 7;
$datetocheck = "2015-02-27 20:00"

The function should return false. If I have

$category = 6;
$datetocheck = "2015-02-27 20:00"

The function should return true.

This https://stackoverflow.com/a/12456356/1092632 looks like what I want to do, but I can't get it to work. Maybe it's not a fitting function.

Any hint/help highly appreciated.

//EDIT I used this function (from above Post)

function in_multiarray($elem, $array,$field)
{
    $top = sizeof($array) - 1;
    $bottom = 0;
    while($bottom <= $top)
    {
        if($array[$bottom][$field] == $elem)
            return true;
        else 
            if(is_array($array[$bottom][$field]))
                if(in_multiarray($elem, ($array[$bottom][$field])))
                    return true;

        $bottom++;
    }        
    return false;
}

But when I use it

in_multiarray("2015-02-27 20:00",$myArray,6)

I get undefined index errors. I tried "in_array()" but this gives me true for 6 AND 7.

Community
  • 1
  • 1
PrimuS
  • 2,085
  • 5
  • 25
  • 48

1 Answers1

1

Try this out :

$array = array(
        array(6 => '2015-02-27 19:00'),
        array(6 => '2015-02-27 20:00'),
        array(6 => '2015-02-27 21:00'));


    function search_in_array($search_in, $search_for){
        foreach($search_in as $arr){
            if(array_key_exists($search_for[0], $arr) 
                && in_array($search_for[1], array_values($arr)))
            return true;
        }

        return false;
    }


    var_dump(search_in_array($array, array(6, '2015-02-27 19:00')));
JC Sama
  • 2,181
  • 1
  • 10
  • 11