Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php的where子句中使用数组_Php_Arrays - Fatal编程技术网

在php的where子句中使用数组

在php的where子句中使用数组,php,arrays,Php,Arrays,我有一个数组$order\u mealsInfo\u id,其值如下 [{“膳食id”:“33”},{“膳食id”:“34”}] “33”和“34”都有相同的值(商店名称、特许经营权id、订单日期时间),所以我只想获取这些值一次,这就是我使用distinct关键字的原因。我想在我的查询中使用$order\u MEALINFO\u id,我的查询是: $get_franchise_info="Select distinct store_name,franchise_id,orde

我有一个数组
$order\u mealsInfo\u id
,其值如下

[{“膳食id”:“33”},{“膳食id”:“34”}]

“33”和“34”都有相同的值(商店名称、特许经营权id、订单日期时间),所以我只想获取这些值一次,这就是我使用distinct关键字的原因。我想在我的查询中使用
$order\u MEALINFO\u id
,我的查询是:

    $get_franchise_info="Select distinct store_name,franchise_id,order_datetime from order_main where id 
IN (".implode(',',$order_mealsInfo_ids['meal_id']).")";
但它给出了这样一个错误:

注意:第94行的C:\xampp\htdocs\learning\service\get\u pending\u orders\u news.php中未定义的索引:fine\u id

警告:内爆():在第94行的C:\xampp\htdocs\learning\service\get\u pending\u orders\u news.php中传递的参数无效


在您的例子中,
$order\u mealsInfo\u id
不是PHP数组。它是一个包含JSON数组的字符串

请试试这个:

$order_mealsInfoJSON = '[{"meal_id":"33"},{"meal_id":"34"}]';
$order_mealsInfoArr = json_decode($order_mealsInfoJSON); // Convert JSON string to PHP array containing objects.

$order_mealsInfoIds = array();
foreach($order_mealsInfoArr as $order_mealsInfo) {
    $order_mealsInfoIds[] = $order_mealsInfo->meal_id;
}

$get_franchise_info="Select distinct store_name,franchise_id,order_datetime from order_main where id IN (".implode(',', $order_mealsInfoIds).")";
[{“膳食id”:“33”},{“膳食id”:“34”}]这不是数组这是json

您需要将json转换为数组

`$array = json_decode($json);
$mapedarray = array_map('current',$array);
$data = implode(',',$mapedarray);
echo $data;
`
所以你得到33,34

`
 $get_franchise_info="Select distinct store_name,franchise_id,order_datetime from order_main where id 
IN (".implode(',',$mapedarray).")";
`

用这个

    $data_jsn = '[{"meal_id":"33"},{"meal_id":"34"}]';
    $data = json_decode($data_jsn,true);
    $data_val = array_map(function($element) {
      return $element['meal_id'];
    }, $data);

    $get_franchise_info="Select distinct store_name,franchise_id,order_datetime from order_main where id 
    IN (".implode(',',$data_val).")";
    $data_jsn = '[{"meal_id":"33"},{"meal_id":"34"}]';
    $data = json_decode($data_jsn,true);
    $data_val = array_map(function($element) {
      return $element['meal_id'];
    }, $data);

    $get_franchise_info="Select distinct store_name,franchise_id,order_datetime from order_main where id 
    IN (".implode(',',$data_val).")";