PHP foreach 进行查询

PHP foreach to make query

我有一个这样的数组

Array ( [12313] => 1 [12312] => 1 ) 1

数组键是仓库中存在的物品,值是物品的数量。

我想使用 php 检查仓库数据库中是否确实存在物品和数量。我想也许我可以对每个项目检查使用 foreach,但我不知道如何使用 foreach。我想进行如下查询:

$query=$this->db->query("SELECT COUNT(*) as amount FROM warehouse where item=12313");
$check=$query->row_array();
$amt=$check['amount'];
if($amt>0){
   return true;
}
else{
   return false;
}
$array=array ( '12313' => 1,'12312' => 1 );
foreach($array as $k=>$val)
{
  $query=$this->db->query("SELECT COUNT(*) as amount FROM warehouse where item='".$k."'");
  $check=$query->row_array();
  $amt=$check['amount'];
  if($amt>0){
    return true;
  }
  else{
   return false;
  }
}

您是否在寻找类似的东西:

$items = array(
        12313 => 1,
        12312 => 1,
    );

foreach ( $items as $key => $value ) : //iterate over array
    $query = "SELECT COUNT(*) as amount FROM warehouse where item = $key";
    $check = $query->row_array();
    $amt = $check['amount'];
    if( $amt>0 ) {
        return true;
    }
    else {
        return false;
    }

endforeach;

try this

$arr = array("[12313]" => "1" ,"[12312]" => "2");
foreach ($arr as  $key=>$value) {
$query="SELECT COUNT(*) as amount FROM warehouse where item='".$key."'";
$check=$query->row_array();
$amt=$check['amount'];
if($amt>0){
return true;
}
else{
return false;
}
}
  $array = array('12313' => 1, '12312' => 1);
            foreach ($array as $key => $value) {

                $this->db->select('COUNT(*) as amount');
                $this->db->from('warehouse');
                $this->db->where('item', $key);
                $result = $this->db->get()->row_array();

                return ($result['amount'] >= $value) ? TRUE : FALSE; //match no of items
             // return ($result['amount'] > 0) ? TRUE : FALSE; //OR if just check the count
            }