Why json showing array index and array object multiple times in eloquent - php

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();

Related

How to get json output from php array

This is my PHP code for json output:
$sql="SELECT id,name FROM languages ORDER BY id";
$result=mysqli_query($conn,$sql);
// Fetch all
$result = mysqli_fetch_all($result,MYSQLI_ASSOC);
$out_put = json_encode($result);
echo $out_put;
This is the json output of the above php code:
{
"0": {
"id": "1",
"name": "English"
},
"1": {
"id": "2",
"name": "Kanada"
},
"2": {
"id": "3",
"name": "Hindi"
},
"3": {
"id": "4",
"name": "Telugu"
}
}
But I want output like this:
{
"Responsecode":200,
"Message":"Sucess",
"languagelist": [
{
"id": "1",
"name": "English"
},
{
"id": "2",
"name": "Kannada"
},
{
"id": "3",
"name": "Hindi"
},
{
"id": "4",
"name": "Telugu"
}
]
}
I am trying to create API and I am new in it. Please help. Thank you in advance.
Just write
$out_put = json_encode([
"Responsecode" => 200,
"Message" => "Sucess",
"languagelist" => $result
]);
You can do it by this way:
$output['Responsecode'] = 200;
$output['Message'] = "Sucess";
$output['languagelist'] = $result;
$out_put = json_encode($output);

difficulty with Conversion of php to json file

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

PHP loop through data from JSON string

I want to get ItemCategory->id's for each Item->id
How can I do it in the best way?
Here is a part of JSON data item
"6": {
"Item": {
"id": "6",
"name": "test",
"description": "description",
},
"ItemThumbnail": null,
"ItemCategory": {
"3": {
"id": "3",
"name": "name",
"status": "active",
"date_created": "2015-07-07 11:23:52",
"date_updated": "0000-00-00 00:00:00",
},
"4": {
"id": "4",
"name": "name",
"status": "active",
"date_created": "2015-07-07 11:23:52",
"date_updated": "0000-00-00 00:00:00",
}
},
"ItemGroup": []
},
You can convert your json string to a PHP array using $array = json_decode($str, true);, then loop your array and extract the information you need.
See http://php.net/manual/en/function.json-decode.php for more detail on json_decode
After json decode use this.
foreach($Item as $Items)
{
$ItemCategory = $Items->ItemCategory;
foreach($ItemCategory as $ItemCategorys)
{
echo $ItemCategorys->id;
}
}

PHP/MongoDB - sort by time

I have the following MongoDB object:
{
"_id": ObjectId("50954f0d13f88da4a590e5ff"),
"uname": "Eamorr",
"1": {
"table": "1",
"color": "red",
"time": NumberInt(1351963491),
"niceTime": "2012-11-03T17: 24: 51+00: 00"
},
"3": {
"table": "3",
"color": "green",
"time": NumberInt(1351963567),
"niceTime": "2012-11-03T17: 26: 07+00: 00"
},
"4": {
"table": "4",
"color": "orange",
"time": NumberInt(1351963506),
"niceTime": "2012-11-03T17: 25: 06+00: 00"
}
}
How do I sort this object by 'time'?
I want table 3 to be first, table 4 to be second and table 1 to be last in the list.
I'm really stuck... Please help!
Update:
I should add, that my server returns the following JSON object:
[{
"table": "4",
"color": "orange",
"time": 1351965770,
"niceTime": "2012-11-03T18:02:50+00:00"
}, {
"table": "3",
"color": "red",
"time": 1351964379,
"niceTime": "2012-11-03T17:39:39+00:00"
}, {
"table": "1",
"color": "red",
"time": 1351964997,
"niceTime": "2012-11-03T17:49:57+00:00"
}]
Is it easier to sort this?
Many thanks,
$coll->find($range, $co)->sort(array('time' => -1) );
Got it...
Using the second JSON (above), I did:
function my_sort($a, $b){
if ($a['time'] > $b['time']) {
return -1;
} else if ($a['time'] < $b['time']) {
return 1;
} else {
return 0;
}
}
usort($obj,'my_sort');
echo json_encode($obj);

PHP Troubles with converting from JSON to stdClass, making changes, and then converting back to JSON

So, in the very beginning, before the send_sms.php is loaded, I have this Json stored in a database:
{
"chats": {
"chat": [{
"id": "1",
"name": "Ethan Wilberforce",
"messages": {
"message": [{
"id": "1",
"name": "Ethan Wilberforce",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Ethan Wilberforce",
"text": "I'm not too bad myself.",
"time": "4:43"
}]
}
}, {
"id": "2",
"name": "Geoff Vahaaho",
"messages": {
"message": [{
"id": "1",
"name": "Geoff Vahaaho",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Geoff Vahaaho",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {
"id": "4",
"name": "Qasim Iqbal",
"text": "Nice.",
"time": "4:43"
}]
}
}]
}
}
The Json is completely valid, no errors. It is storing two chats, with messages in them.
Now, here is the PHP code that alters the Json:
$data = $user->data;
$parsed_data = json_decode($data);
...
for($i = 0, $size = sizeof($parsed_data->chats->chat); $i < $size; ++$i) {
if($parsed_data->chats->chat[$i]->name == $to) {
$found = true;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->name = $user->name;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->text = $message;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->time = $time;
echo "done. ";
break;
}
}
What I intend this to do is, to add another stdClass object in the "message" array of the chat. So I basically do just that, hoping it will work.
Now, it works, kind of, but here is the new Json after we json_encode it:
{
"chats": {
"chat": [{
"id": "1",
"name": "Ethan Wilberforce",
"messages": {
"message": [{
"id": "1",
"name": "Ethan Wilberforce",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Ethan Wilberforce",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {}, {
"id": 4
}, {
"name": "Qasim Iqbal"
}, {
"text": "Hello i am testing"
}, {
"time": 1326066200
}]
}
}, {
"id": "2",
"name": "Geoff Vahaaho",
"messages": {
"message": [{
"id": "1",
"name": "Geoff Vahaaho",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Geoff Vahaaho",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {
"id": "4",
"name": "Qasim Iqbal",
"text": "Nice.",
"time": "4:43"
}]
}
}]
}
}
You will notice it was indeed added in the "Ethan Wilberforce" chat, but each string in the stdClass was converted to its own array item in the "message" array. How could I fix this problem? Many thanks.
Your problem is this:
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);
It basically amounts to:
$array[$last] = new stdClass();
$array[$last+1] = "id";
$array[$last+2] = "name";
You keep appending new arrays/objects, because you use sizeof(...) which always becomes one larger than the previous line. It's not the last index, but the size. Which is $lastindex+1.
What you should be doing anyway, is not using an object, but just appending an array, and with all its attributes at once:
$parsed_data->chats->chat[$i]->messages->message[] = array(
"id" => ...,
"name" => ...,
"time" => ...,
);
When you encode that associative array back into JSON it will also become a normal {...} JSON object group.

Categories