php - `json_encode` not working with explode -
i have string containing numbers separated comma. ie, 1,2,3,6...
. have removed comma using explode. want match corresponding values in database.
my code is,
$color = "1,2,3,9,5"; $color_split = explode(",", $color); foreach($color_split $item) { $select_color = "select * tbl_product_color color_id = '$item'"; $select_color_q = mysqli_query($c, $select_color) or die(mysqli_error($c)); $length = mysqli_num_rows($select_color_q); if($length > 0) { while($select_color_r = mysqli_fetch_object($select_color_q)) { $var[] = $select_color_r; } $var = json_encode($var); echo '{"color_list":'.$var.'}'; } else { $arr = array('status'=>"notfound"); echo '{"color_list":['.json_encode($arr).']}'; } }
now output is,
{"color_list":[{"color_id":"1","color_name":"white","color_code":"#f2f2f2"}]} , 1 error, fatal error: [] operator not supported strings in c:\wamp\www\jithin\get_color.php on line 18
line 18 contains $var[] = $select_color_r;
my required output is,
{"color_list":[{"color_id":"1","color_name":"white","color_code":"#f2f2f2"},{"color_id":"2","color_name":"black","color_code":"#000000"},{"color_id":"3","color_name":"red","color_code":"#f000000"},...]}
you don't need use foreach
, explode
on here can update mysql
query , code
as
$color = "1,2,3,9,5"; $select_color = "select * tbl_product_color color_id in ($color)"; $select_color_q = mysqli_query($c, $select_color) or die(mysqli_error($c)); $arr['color_list'] = array(); if(mysqli_num_rows($select_color_q) > 0) { while($select_color_r = mysqli_fetch_object($select_color_q)) { $var[] = $select_color_r; } }else{ $var = array('status'=>"notfound"); } $arr['color_list'] = $var; echo json_encode($arr);
Comments
Post a Comment