-5

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

[{
    "0": "1",
    "id": "1",
    "1": "Hakob",
    "name": "Hakob",
    "2": "[email protected]",
    "email": "[email protected]"
}, {
    "0": "2",
    "id": "2",
    "1": "Arsen",
    "name": "Arsen",
    "2": "[email protected]",
    "email": "[email protected]"
}]

I don't want to see the following key/values "0": "1","1": "Hakob","2": "[email protected]", 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);
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Or use mysqli_fetch_assoc()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.