1

I have an array $order_mealsInfo_ids which has the values like this

[{"meal_id":"33"},{"meal_id":"34"}]

Both the meal_id "33" and "34" has the same values(store_name,franchise_id,order_datetime) so I only want to get those values once that's why I am using distinct keyword.I want to use $order_mealsInfo_ids in my query,My query is :

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

But its giving this error:

Notice: Undefined index: meal_id in C:\xampp\htdocs\learning\service\get_pending_orders_news.php on line 94

Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\learning\service\get_pending_orders_news.php on line 94

3 Answers 3

2

In your case $order_mealsInfo_ids is not an PHP array. It's a string containing a JSON array.

Please try this:

$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).")";
Sign up to request clarification or add additional context in comments.

Comments

1

[{"meal_id":"33"},{"meal_id":"34"}] this is not Array this is json

you need to convert json to array

`$array = json_decode($json);
$mapedarray = array_map('current',$array);
$data = implode(',',$mapedarray);
echo $data;
`

so you get 33,34

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

or

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

Comments

0

Use this

    $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).")";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.