I have simple problem. I am fetching data from database, and I am sending it to Javascript client over Ajax. I am having trouble with formatting array that needs to be sent. My code:
$queryData = $this->execute_query("SELECT tema, opis, id FROM predlogtema WHERE (idpred = '$oblast' AND idprof = '$profesor' AND odabrana = '0')",1);
foreach ($queryData as $key => $value) {
$tema=$queryData[$key]['tema'];
$opis=$queryData[$key]['opis'];
$id=$queryData[$key]['id'];
$profesors[] = array
(
'tema' => ($tema),
'opis' => ($opis),
'id'=>($id)
);
}
echo json_encode($profesors);
Array $queryData looks like this:
Array ( [0] => Array ( [tema] => tema1 [opis] => opis [id] => 1 ) [1] => Array ( [tema] => tema1 [opis] => fsdfds [id] => 4 ) [2] => Array ( [tema] => fsd [opis] => fsdf [id] => 6 ) [3] => Array ( [tema] => ewqrwqr [opis] => etwretrewtre [id] => 21 ) [4] => Array ( [tema] => ewqre [opis] => rewtrwert [id] => 23 ) [5] => Array ( [tema] => ava [opis] => aba [id] => 26 ) [6] => Array ( [tema] => prob1 [opis] => prrobaaa [id] => 30 ) )
Line echo json_encode($profesors) gives this:
Array
(
[0] => Array
(
[tema] => tema1
[opis] => opis
[id] => 1
)
[1] => Array
(
[tema] => tema1
[opis] => fsdfds
[id] => 4
)
[2] => Array
(
[tema] => fsd
[opis] => fsdf
[id] => 6
)
[3] => Array
(
[tema] => ewqrwqr
[opis] => etwretrewtre
[id] => 21
)
[4] => Array
(
[tema] => ewqre
[opis] => rewtrwert
[id] => 23
)
[5] => Array
(
[tema] => ava
[opis] => aba
[id] => 26
)
[6] => Array
(
[tema] => prob1
[opis] => prrobaaa
[id] => 30
)
)
[{"tema":"tema1","opis":"opis","id":"1"},{"tema":"tema1","opis":"fsdfds","id":"4"},{"tema":"fsd","opis":"fsdf","id":"6"},{"tema":"ewqrwqr","opis":"etwretrewtre","id":"21"},{"tema":"ewqre","opis":"rewtrwert","id":"23"},{"tema":"ava","opis":"aba","id":"26"},{"tema":"prob1","opis":"prrobaaa","id":"30"}]
However it should look only like this:
[{"tema":"tema1","opis":"opis","id":"1"},{"tema":"tema1","opis":"fsdfds","id":"4"},{"tema":"fsd","opis":"fsdf","id":"6"},{"tema":"ewqrwqr","opis":"etwretrewtre","id":"21"},{"tema":"ewqre","opis":"rewtrwert","id":"23"},{"tema":"ava","opis":"aba","id":"26"},{"tema":"prob1","opis":"prrobaaa","id":"30"}]
What should I change in my code to achieve this?
Can you try that:
$queryData = $this->execute_query("SELECT tema, opis, id FROM predlogtema WHERE (idpred = '$oblast' AND idprof = '$profesor' AND odabrana = '0')",1);
foreach ($queryData as $key => $value) {
$tema=$queryData[$key]['tema'];
$opis=$queryData[$key]['opis'];
$id=$queryData[$key]['id'];
$profesors[] = array
(
'tema' => utf8_encode($tema),
'opis' => utf8_encode($opis),
'id'=> utf8_encode($id)
);
}
echo json_encode($profesors);
What you are doing seems a bit odd here. You are iterating over a multi-dimensional array, pulling the values out by key and then adding the values to a new array using the same keys and building them back into a multi-dimensional array. The array you begin with should be identical to the array you end with unless there is something else missing from the code here.
That said the format you present should work as expected. If your $professors array structure looks like:
$array = array(
array(
'tema' => 'tema1',
'opis' => 'opis',
'id' => 1
),
array(
'tema' => 'tema1',
'opis' => 'fsdfds',
'id' => 4
),
array(
'tema' => 'fsd',
'opis' => 'fsdf',
'id' => 6
),
array(
'tema' => 'fsd',
'opis' => 'fsdf',
'id' => 6
),
array(
'tema' => 'ewqrwqr',
'opis' => 'etwretrewtre',
'id' => 21
),
array(
'tema' => 'ewqre',
'opis' => 'rewtrwert',
'id' => 23
),
array(
'tema' => 'ava',
'opis' => 'aba',
'id' => 26
),
array(
'tema' => 'prob1',
'opis' => 'prrobaaa',
'id' => 30
));
Your ouput when using:
echo json_encode($professors);
Should be:
[{"tema":"tema1","opis":"opis","id":1},{"tema":"tema1","opis":"fsdfds","id":4},{"tema":"fsd","opis":"fsdf","id":6},{"tema":"fsd","opis":"fsdf","id":6},{"tema":"ewqrwqr","opis":"etwretrewtre","id":21},{"tema":"ewqre","opis":"rewtrwert","id":23},{"tema":"ava","opis":"aba","id":26},{"tema":"prob1","opis":"prrobaaa","id":30}]
The following code will give you the expected result:
foreach ($queryData as $key => $value) {
$profesors[] = array
(
'tema' => $value['tema'],
'opis' => $value['opis'],
'id'=> $value['id']
);
}
echo json_encode($profesors);