我有一个如下所示的数组:
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 ) )
我需要检查一下" game_id"存在于该数组中。例如,如果 1642828
我目前的PHP代码如下:
// 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);
}
所以我最初的尝试是这样的:
if(!in_array($game_id, $betslip)
{
// Add To Slip
}
这是不是有办法在多维数字阵列上进行if in_array?
由于
答案 0 :(得分: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';
答案 1 :(得分:0)
function in_array($ar, $k){
$isFound = false;
$array = $ar[0];
foreach($array as $key => $value){
if($key == $k){
$isFound = true;
break;
}
}
return $isFound;
}