-5

Hi I have the following JSON data ,fetched from my database:

[{
    "0": "1",
    "id": "1",
    "1": "Hakob",
    "name": "Hakob",
    "2": "abc@email.com",
    "email": "abc@email.com"
}, {
    "0": "2",
    "id": "2",
    "1": "Arsen",
    "name": "Arsen",
    "2": "zxc@email.com",
    "email": "zxc@email.com"
}]

I don't want to see the following key/values "0": "1","1": "Hakob","2": "abc@email.com", and same for other row. Who does now what are they, and how I can remove them.

Here is my PHP script for getting this thing

$sql = "SELECT * FROM contacts";
$result = mysqli_query($connect, $sql);
$response = array();
while ($row = mysqli_fetch_array($result)) {
    $response[] = $row;
}

print json_encode($response);

// Close connection
mysqli_close($connect);
localheinz
  • 7,720
  • 2
  • 29
  • 39
  • Possible duplicate of [remove item from array using its name / value](https://stackoverflow.com/questions/6310623/remove-item-from-array-using-its-name-value) – Abid Aug 08 '17 at 09:11

1 Answers1

1

You simply have to pass second params of mysqli_fetch_array to get desired result

$sql = "SELECT * FROM contacts";
$result = mysqli_query($connect, $sql);
$response = array();

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { //<-----------change this
    $response[] = $row;
}

print json_encode($response);

// Close connection
mysqli_close($connect);

EDIT

OR you can use mysqli_fetch_assoc($result) to get associative array

See the manual : http://php.net/manual/en/mysqli-result.fetch-array.php

http://php.net/manual/en/mysqli-result.fetch-assoc.php

localheinz
  • 7,720
  • 2
  • 29
  • 39
B. Desai
  • 16,092
  • 5
  • 22
  • 43