I have the following table:
-> +-----+---------+-----+-------+--------------+
-> | id1 | fname | id2 | fname | relationship |
-> +-----+---------+-----+-------+--------------+
-> | 4 | Albaraa | 5 | Sadi | Father |
-> +-----+---------+-----+-------+--------------+
I assign the following to the variable $relations:
$relations = $stmt->fetchAll(PDO::FETCH_ASSOC);
Finally I print out the JSON encode of this variable using this:
echo json_encode($relations);
...and I get the following output:
[{"id1":"4","fname":"Sadi","id2":"5","relationship":"Father"}]
So here is a few questions about the output that I get:
1) You can see that my output does not seem right as it should be something like this:
[{"id1":"4","fname":"Albaraa","id2":"5","fname":"Sadi","relationship":"Father"}]
Why is it not printing correctly, does it have something to do with the fact that the table, has two columns with the same name "fname"? If there is any input on how to fix this that would be amazing!
2) Another thing I was curious to know is that I actually want to change the JSON to where it becomes something like this:
[{"id":"4","name":"Albaraa","id":"5","name":"Sadi","relationship":"Father"}]
Is there an easy way to change "id1" and "id2" to become "id" and both "fname" to become just "name"?
Again any input on this will be greatly appreciated!