Zedkay22 Zedkay22 - 2 months ago 8
JSON Question

Array Key 0 instead of the string only for JSON encode

I'm attempting to use json_encode on a php array. I have to structure the returned JSON as:

[
{"text": "Title1"},
{"text": "URL"}
]


I've tried the following, but I keep getting 0 as a key.

$xml = simplexml_load_file($url);

$title1 = $xml->results->result->jobtitle;
$snippet1 = $xml->results->result->snippet;
$url1 = $xml->results->result->url;




$arrays = array('text'=>$title1);
echo json_encode($arrays);


What am I doing wrong with my encoded array? How do I have it so that it doesn't return as 0?

{"text":{"0":"CDL-A Dry Bulk Drivers Wanted - Regional - OH, WV, PA"}}

Answer

Please try this: You have no mistake in json_encode.

$title1 = $xml->results->result->jobtitle;

...

$arrays = array('text'=>$title1[0]);