PHP JSON Array Objects adding 0 as key
166
UPDATED QUESTION
Here is my class...
class EbayItem
{
public $ebayItemId;
}
Right before my foreach loop, I have the following:
$mainResponse = array();
$mainResponse = [
[
"ack" => "success",
],
"result" => []
];
Inside of my loop, I have...
$ebayItem = new EbayItem();
$ebayItem->ebayItemId = $itemId;
array_push($mainResponse['result'], $ebayItem);
Right after my loop I have...
echo json_encode($mainResponse);
This will produce the following:
{"0":{"ack":"success"},"result":[{"ebayItemId":{"0":"153532419741"}}]}
I need it to produce...
{"ack": "success","result": [{"ebayItemId": "153532419741","ebayItemTitle": "..."}]}
Answer
Solution:
You can't.
This is not valid JSON.
{}
) contain"key": value
pairs.[]
) contain an ordered list of values.You can't have a key (
"result"
) in an array.re edit:
You can't have a value without a key in an object either.