I'm using PHP+MySQL+ADODB to fetch an array from the database and then convert it to JSON. However, the object ends up with duplicate entries (associative and numeric).
How would I go about returning only the associative rows?
$sql = 'SELECT * FROM table';
$rs = $conn->execute($sql);
$result = $rs->getrows();
echo json_encode($result);
// OUTPUT
{
"0": "18556",
"VID": "18556",
"1": "1",
"UID": "1",
"2": "Title of entry",
"title": "Title of entry",
"3": "0",
"likes": "0",
"4": "0",
"dislikes": "0"
}
Related
I want to reindex my array both key and values in PHP,
example if delete data: "2" , data 3 will be reindex to 2 and values inside of array 3 {sequence : 3 "ssdummy tossssdo 3"} will be change to {sequence : 2 "ssdummy tossssdo 3"}
{
"sequence": 2,
"data": {
"1": {"sequence": "1", "todo": "dummy todo"},
"2": {"sequence": "2", "todo": "dummy todo 2"},
"3": {"sequence": "3", "todo": "ssdummy tossssdo 3"},}
}
First, you can convert your values to a array, as the input is a json. Then parse the array, to remove the required value.
After parsing the array, you can convert it back to json (if it is needed).
The next script remove the value indicated in the $valueToRemove variable.
<?php
$jsonValues = '{
"sequence": 2,
"data": {
"1": {"sequence": "1", "todo": "dummy todo"},
"2": {"sequence": "2", "todo": "dummy todo 2"},
"3": {"sequence": "3", "todo": "ssdummy tossssdo 3"}
}
}';
$valueToRemove = 2;
$arrayValues = json_decode($jsonValues, true);
$oldData = $arrayValues['data'];
$newData = array();
$counter = 0;
foreach ($oldData as $oldIndex => $oldValue) {
if ($valueToRemove != $oldIndex) {
$counter++;
$newData[$counter] = array(
'sequence' => $counter,
'todo' => $oldValue['todo']
);
}
}
$arrayValues['data'] = $newData;
$jsonValues = json_encode($arrayValues);
var_dump($jsonValues);
?>
I have the following output from the script:
{"sequence":2,"data":{"1":{"sequence":1,"todo":"dummy todo"},"2":{"sequence":2,"todo":"ssdummy tossssdo 3"}}}
guys help me I'm a bit confused how my code producing an extra property to my json result see below:
$connect=mysqli_connect("localhost", "root", "", "thecmlco_widget");
$query = "SELECT * FROM widgetdb";
$result = mysqli_query($connect,$query);
$rows = array();
while($r = mysqli_fetch_array($result)) {
$rows[] = $r;
}
echo json_encode($rows);
mysqli_close($connect);
here is the result with an extra property 0 , 1 , 2 , 3 , 4 ,5 and I don't know why its creating those extra property.
[
{
"0": "1",
"1": "Vacation",
"2": "http://trilogy.editor.multiscreensite.com/preview/dm-theme-1000772-en-291",
"3": "https://dd-cdn.multiscreensite.com/themes-panel/preview/vacation.jpg",
"4": "1000772",
"5": "0",
"id": "1",
"template_name": "Vacation",
"preview_url": "http://trilogy.editor.multiscreensite.com/preview/dm-theme-1000772-en-291",
"thumbnail_url": "https://dd-cdn.multiscreensite.com/themes-panel/preview/vacation.jpg",
"templade_id": "1000772",
"can_build_from_url": "0"
},
{
"0": "2",
"1": "Product",
"2": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"3": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"4": "1003040",
"5": "0",
"id": "2",
"template_name": "Product",
"preview_url": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"thumbnail_url": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"templade_id": "1003040",
"can_build_from_url": "0"
}
]
Use mysqli_fetch_assoc() instead of mysqli_fetch_array() function to get rows as associative array
I'm trying to create a two-dimensional array with the following code, but it gets JSON-encoded as an object. How do I fix this?
$result = $bd->query("select * from contenidos where idfolleto=$idf order by fila");
$arr = array();
if ($result) {
while ($row = mysqli_fetch_object($result)) {
$filaAct = $row->fila;
$arr[$filaAct][] = (array) $row;
}
}
echo json_encode($arr);
The output is:
{
"1": [{
"id": "6",
"idfolleto": "1",
"fila": "1",
"orden": "1",
"tipo": "carrousel",
"titulo": "",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}],
"2": [{
"id": "7",
"idfolleto": "1",
"fila": "2",
"orden": "1",
"tipo": "texto-imagenes",
"titulo": "Texto 1",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}, {
"id": "8",
"idfolleto": "1",
"fila": "2",
"orden": "2",
"tipo": "texto-imagenes",
"titulo": "Texto 2",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}],
"3": [{
"id": "9",
"idfolleto": "1",
"fila": "3",
"orden": "3",
"tipo": "texto-imagenes",
"titulo": "Texto 3",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}]
}
You would have to index it without using your own defined indices.
If you must maintain using fila as the indices then you have to understand that the encoded JSON will be an object. You can json_decode it back into an array though like: json_decode($json, 1);
Otherwise before encoding the array you could call: $arr = array_values($arr); which would result in the array being encoded as a JSON array once you call json_encode.
If you're sure that your fila fields will contain "indexes" (I mean continuous integers starting from 0), then you can convert them to integers using intval.
if ($result) {
while ($row = mysqli_fetch_object($result)) {
$arr[intval($row->fila)][] = (array) $row;
}
}
Otherwise, it probably would be better to just group the records and create an array of the groups.
if ($result) {
$groups = array();
while ($row = mysqli_fetch_object($result)) {
$groups[$row->fila][] = (array) $row;
}
$arr = array_values($groups);
}
I have been tiring to convert the PHP to JSON file but the JSON file generated is very different then normally occur.
The table is present below
rid rname mobile email address opentiming closetiming menuid type averagecost image
1 abc 9876543212 sbjaca#gmail.com fdsjdsfdnm 00:10:00 00:00:00 1 asian 120 http://gjsblog.esy.es/images/download.png
2 abcdefc 9876543212 ajit#gmail.com qwertym 00:00:03 00:00:04 2 chinese 120 http://gjsblog.esy.es/images/The_Table.png
The php file is present below
//retrieve.php
<?php
include("dbconfig.php");
$result = #mysql_query("select * from Restaurants ");
$response =array();
if(#mysql_num_rows($result)>0){
$response['Restaurants'] = array();
while($row=#mysql_fetch_array($result)){
array_push($response['Restaurants'], $row);
}
}
if($result){
$response['success']=1;
$response['message']="Records Retrieved sucessfully";
}else{
$response['success']=0;
$response['message']="Retrieval Failure";
}
echo json_encode($response);
?>
The JSON contains the data in the column twice, once before the column name and once after the column name. The JSON appears as
{
"Restaurants": [
{
"0": "1",
"rid": "1",
"1": "abc",
"rname": "abc",
"2": "9876543212",
"mobile": "9876543212",
"3": "sbjaca#gmail.com",
"email": "sbjaca#gmail.com",
"4": "fdsjdsfdnm",
"address": "fdsjdsfdnm",
"5": "00:10:00",
"opentiming": "00:10:00",
"6": "00:00:00",
"closetiming": "00:00:00",
"7": "1",
"menuid": "1",
"8": "asian",
"type": "asian",
"9": "120",
"averagecost": "120",
"10": "http:\/\/gjsblog.esy.es\/images\/download.png",
"image": "http:\/\/gjsblog.esy.es\/images\/download.png"
},
{
"0": "2",
"rid": "2",
"1": "abcdefc",
"rname": "abcdefc",
"2": "9876543212",
"mobile": "9876543212",
"3": "sbjaca#gmail.com",
"email": "sbjaca#gmail.com",
"4": "fdsjdsfdnm",
"address": "fdsjdsfdnm",
"5": "00:00:03",
"opentiming": "00:00:03",
"6": "00:00:04",
"closetiming": "00:00:04",
"7": "2",
"menuid": "2",
"8": "chinese",
"type": "chinese",
"9": "120",
"averagecost": "120",
"10": "http:\/\/gjsblog.esy.es\/images\/The_Table_(restaurant)_logo.png",
"image": "http:\/\/gjsblog.esy.es\/images\/The_Table_(restaurant)_logo.png"
}
],
"success": 1,
"message": "Records Retrieved sucessfully"
}
Change
while($row=#mysql_fetch_array($result))
To
while($row=#mysql_fetch_array($result, MYSQL_ASSOC))
Because if you just use the first statement, MYSQL_BOTH will be used as default, and make the result array like that.
Just a suggestion, you better use MySQLi or PDO_MySQL, because mysql_fetch_array has been deprecated in PHP 5.5.0 and removed in PHP 7.0.0
My JSON output is like:
{
"0": "1",
"1": "araer",
"2": "aeraer",
"3": "aeraer",
"4": "News/Magzine Website",
"5": "2016-01-22 13:15:56",
"6": "2016-01-22 13:15:56",
"id": "1",
"name": "araer",
"email": "aeraer",
"url": "aeraer",
"web": "News/Magzine Website",
"created_at": "2016-01-22 13:15:56",
"updated_at": "2016-01-22 13:15:56"
}, {
"0": "2",
"1": "asd",
"2": "asd",
"3": "sfd",
"4": "sdf",
"5": "2016-02-10 13:06:28",
"6": "0000-00-00 00:00:00",
"id": "2",
"name": "asd",
"email": "asd",
"url": "sfd",
"web": "sdf",
"created_at": "2016-02-10 13:06:28",
"updated_at": "-0001-11-30 00:00:00"
}
The code I am using in Model:
public function getBlog() {
try {
$result = $this - > get();
return $result;
} catch (Exception $ex) {
return array();
}
return array();
}
Check PDO fetch style in your config/databse.php array. If it is not present there then it will use PDO::FETCH_BOTH (by default) which returns both associative and numeric values. To get only associative value you need to set 'fetch' => PDO::FETCH_CLASS, or 'fetch' => PDO::FETCH_ASSOC
You function will retutn all records from your blog table.
And if you want to get single record then try something like
$this->get()->first();