[Solved] Turning a SQL row into an array


If you only want specific associative properties from all columns queried from the MySQL call, just set them in an array with their respective properties:

$categoriesTest = array();
$sql    = "SELECT * FROM `respondent_data` WHERE `respondent_firstname` = 'John'";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
    $categoriesTest[] = array(
        'respondent_sdo'    => $row['respondent_sdo'],
        'respondent_dcto'   => $row['respondent_dcto'],
        'respondent_ed'     => $row['respondent_ed'],
        'respondent_ca'     => $row['respondent_ca'],
        'respondent_dhpt'   => $row['respondent_dhpt'],
        'respondent_irt'    => $row['respondent_irt'],
        'respondent_gl'     => $row['respondent_gl'],
        'respondent_il'     => $row['respondent_il']
    );
}
$categoriesTest = json_encode($categoriesTest); // get JSON

However, if you wanted to keep all columns queried, but create custom elements you’ll want to use array_merge:

$categoriesTest = array();
$sql    = "SELECT * FROM `respondent_data` WHERE `respondent_firstname` = 'John'";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
    $categoriesTest[] = array_merge($row, array(
        'custom_value_1'    => 'test',
        'custom_value_2'    => 'test2'
    ));
}
$categoriesTest = json_encode($categoriesTest); // get JSON

solved Turning a SQL row into an array