-1

I am trying to create a key and value array from database record, but the array just getting the final record! Here is a snippet of my code:

$cateogiresArr = array();
while ($row = mysqli_fetch_row($result))
{
  $cateogiresArr["categoryname"] = $row[1];
  $cateogiresArr["description"] = $row[2];
}
header("Content-type:application/json");
$json_categories = json_encode($cateogiresArr);
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37

1 Answers1

4

On each iteration add new array with required data to $cateogiresArr:

while ($row=mysqli_fetch_row($result))
{
    $cateogiresArr[] = [
        "cateogryname" => $row[1],
        "description" => $row[2],
    ];
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • 1
    @PhillipMark Please mark this answer as accepted by clicking the green tick on the left. Also, some spelling corrections: `categoryname`, and `categoriesArr`. – JustCarty Apr 06 '19 at 13:18