If i do this :
$new_arr = array(
0 => 'keyboard',
1 => 'mouse',
2 => 'computer'
);
print_r(json_encode($new_arr));
Output:
["keyboard","mouse","computer"]
But say i fetch all rows of "product" table from my database and i do this :
$product_with_id_map = array();
foreach($query as $result) {
$product_with_id_map[$result->id] = $result->name;
}
print_r(json_encode($product_with_id_map));
Output:
{"0":"Keyboard","1":"mouse","2":"computer"}
I really need to retain the key of the array when i json_encode also can you tell me how to achieve the second output in the 1st example ?