Mehmet Genç Mehmet Genç -4 years ago 97
PHP Question

insert element PDO result array

This is my code :



$stmt = $db->prepare("SELECT * FROM wishlists WHERE user_id='1'");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$res = array('saved_id' => "1");
array_push($result, $res);
echo json_encode($result);





end result:

[{"id":"1","user_id":"1","name":"San Francisco","privacy":"0","pick":"Yes"},{"saved_id":"1"}]


But i want show saved_id in first array for example:

[{"id":"1","user_id":"1","name":"San Francisco","privacy":"0","pick":"Yes","saved_id":"1"}]


you can help me?

Answer Source

Change

array_push($result, $res);

To

$result = array_map( function($r) use($res){
    return array_merge($r, $res);
}, $result);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download