0

I have an array that looks as follows :

Array
(
    [0] => Array
        (
            [bet_type] => 1
            [game_id] => 1627895
            [bet_id] => 1
            [team] => Paris SG
            [odds] => 29/100
            [line] => 0
        )

    [1] => Array
        (
            [bet_type] => 2
            [game_id] => 1642828
            [bet_id] => 1
            [team] => Derby County
            [odds] => 19/10
            [line] => 0
        )

)

I need to check if a "game_id" exists within that array. So for example if 1642828

My current PHP code is as follows :

    // Build An array titled Bet
    $bet = array(
        '0' => array(
            'bet_type'  =>  $bet_type,
            'game_id'   =>  $game_id,
            'bet_id'    =>  $bet_id,
            'team'      =>  urldecode($teamname),
            'odds'      =>  $odds,
            'line'      =>  $bet_line
        )
    );

    $betslip = $this->session->userdata('betslip');

    // Create The Betslip For The First Time...
    if(empty($betslip))
    {
        $this->session->set_userdata('betslip', $bet);
    }
    else
    {
        // Add To The Betslip Array...
        $betslip[] = array(
            'bet_type'  =>  $bet_type,
            'game_id'   =>  $game_id,
            'bet_id'    =>  $bet_id,
            'team'      =>  urldecode($teamname),
            'odds'      =>  $odds,
            'line'      =>  $bet_line
        );

        $this->session->set_userdata('betslip', $betslip);
    }

So my initial try was this :

if(!in_array($game_id, $betslip)
{
  // Add To Slip
}

This isn't working is there a way to do an if in_array on a multidimensonal array?

Thanks

StuBlackett
  • 3,480
  • 15
  • 60
  • 102
  • possible duplicate of [in\_array() and multidimensional array](http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array) – Dallin Mar 19 '15 at 17:49

2 Answers2

0
function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';

in_array() and multidimensional array

Community
  • 1
  • 1
Sam Battat
  • 5,647
  • 1
  • 18
  • 28
0
function in_array($ar, $k){
    $isFound = false;
    $array = $ar[0];
    foreach($array as $key => $value){
         if($key == $k){
              $isFound = true;
              break;
         }
    }
    return $isFound;
}
UFFAN
  • 626
  • 2
  • 7
  • 23