The problem is here: I'm trying to write a code which would make a dynamic PHP array. I have an empty array, to which need to add keys with their values, values being passed by a do-while loop from the MySQL server. The following php code:
$result=mysqli_query($link,"SELECT products_id,voted,rating FROM table_products $sorting LIMIT 6");
if(mysqli_num_rows($result)>0) {
$a = array();
do {
$row = mysqli_fetch_array($result);
$obj=$row['products_id'];
$rating=$row['rating'];
$voted=$row['voted'];
array_push($a,array('obj'.$obj => array('rating' =>
$rating, 'voted' => $voted)));
} while ($row = mysqli_fetch_array($result)
);}
$json = json_encode($a);
echo $json;
...gives me this json string:
[{"obj1":{"rating":"25","voted":"5"}},{"obj3":{"rating":"36","voted":"10"}},{"obj5":{"rating":"6","voted":"4"}}]
...but I need this:
["obj1":{"rating":"25","voted":"5"},"obj3":{"rating":"36","voted":"10"},"obj5":{"rating":"6","voted":"4"}]
So how can I push this:
'obj'.$obj => array('rating' => $rating, 'voted' => $voted)
whithout putting it in a separate array, like I did in my code? I just don't know a right way to implement this.