javascript - get json_encode data without an index number -


i'm passing db values using json _encode(). below query , works okay.

but when add additional data array number in front of output data.

$sql = "select item, price, availability           items             category = :category               order item desc"; $stmt= $connect->prepare($sql); $stmt->execute(array(':category'=>"fruits")); $rslt = $stmt->fetchall(pdo::fetch_assoc);  foreach ($rslt $val){   $data[] = $val;  } $data['additional'] = $someadditionaldata; echo json_encode($data); 

current output:

{"0":{"item":"lychee"},"1":{"item":"ornage"},"2": "item":"apple"},"additional":abc} 

my question is, how output without index numbers (as below)

[{"item":"lychee"},{"item":"ornage"},{"item":"apple"},{"additional":"abc"}] 

php chooses serialise array object when any array key not numeric or when array has missing indices.

you can either push element on existing array:

$data[] = ['additional' => $someadditionaldata]; 

or, perhaps better, pick different structure:

$items = []; foreach ($rslt $val){   $items[] = $val;  } echo json_encode([     'items' => $items,     'additional' => $someadditionaldata; ]); 

in javascript, can access items using data.items , additional data via data.additional.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -