[英]If in_array on multidimensional array
我有一個數組,如下所示:
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?
謝謝
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';
function in_array($ar, $k){
$isFound = false;
$array = $ar[0];
foreach($array as $key => $value){
if($key == $k){
$isFound = true;
break;
}
}
return $isFound;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.