I have this
[{"cname":"NCPB"},{"cname":"NBK"}]
Which I'm getting from json_encode
on mysql result
How can I achive this ["NCPB","NBK"]
From JSON OBJECT ABOVE.
I have this
[{"cname":"NCPB"},{"cname":"NBK"}]
Which I'm getting from json_encode
on mysql result
How can I achive this ["NCPB","NBK"]
From JSON OBJECT ABOVE.
I guess I can answer this without seeing your exact code. Basically, you're currently fetching your query results with something like this and using json_encode
on the result of that:
while ($row = $query->some_fetch_method()) {
$results[] = $row; // you're building an array of associative arrays
}
echo json_encode($results);
Instead, just fill the results array with that specific column instead of the entire row.
while ($row = $query->some_fetch_method()) {
$results[] = $row['cname']; // you need to build an array of strings instead
}
echo json_encode($results);
You should get the array of query results into the format you want before you use json_encode
. Otherwise, you'll have to decode the JSON string you just made, reformat the result of that, and reencode it:
$json = json_encode(array_column(json_decode($json), 'cname'));
or use some weird regex or something. It doesn't seem to make sense to undo what you've already done, though.
If you don't get what I mean, edit your question to add the code that fetches your query results, and I'll show you an example based on your exact code, but this should give you the general idea.