I trying to paginate my JSON response but got error like this
Call to undefined method stdClass::count()
My JSON response from the Laravel API by using guzzle ......
here is my controller code
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$total = $content->count();
$paginationRecord = CollectionPaginate::paginate($content, $total, '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord->data]);
}
$content = json_decode($response->getContents());
$total = $content->count();
I am not entirely sure why you think the result of json_decode would have a count method? The JSON decoding always results in a generic object (stdClass) since there's no way for the PHP interpreter to know it represents an available class.
The ->count method is available on Countable implementations (such as ArrayCollection). If you expect a Countable class, then you can either have a factory to build your object from JSON or try to cast the stdClass to ArrayCollection.
Otherwise, if your JSON data is a valid array, you can try to use
$decoded = json_decode($data, true)
meaning it will decode it to an array rather than an object, which enables you to do
count($decoded)
$content is an object, not a collection or array you can use php method count with array get $total
Please Change to
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents(),true );
$total = count($content);
$paginationRecord = CollectionPaginate::paginate($content, $total, '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord->data]);
}
json_encode converts an object to:
{
"height":10,
"width":20,
"depth":5
}
But I need it to include the objects class name as well:
{
"cuboid":
{
"height":10,
"width":20,
"depth":5
}
}
public function toJson() {
return json_encode([get_class($this) => $this]);
}
Hope this will help you out. Here we are converting json to array and putting it into an array of field cuboid
Try this code snippet here
<?php
ini_set('display_errors', 1);
$json='{
"height":10,
"width":20,
"depth":5
}';
$result["cuboid"]=json_decode($json,true);
echo json_encode($result,JSON_PRETTY_PRINT);
You can use get_class to get class name of an object.
$json = json_encode(array(get_class($object) => $object));
Rules for json_encode
if you have an object it will be encode to {'pro1':'value'}
if you have an array it will be encode to ['value']
if you have an string it will be encode to 'value'
Note: If you have an assoc-array in php, it becomes an object in json! If you have an index-array it will be an array in json. Dont mix index & assoc!
Test this bad pratice: echo json_encode(array('foo' => 'bar',1,2)); Result is this bad syntax {"kitten":"test","0":1,"1":2} (propertie-names should NOT be numbers !!!)
So if you want and object under a property name do this
$obj = new stdClass();
$obj->prop = array(1,2,'a');
$newObject = new stdClass();
$newObject->objname = $obj;
print_r(json_encode($newObject));
Becomes: {'objname':{'prop':[1,2,'a']}}
Have a nice day :-)
I have a problem where I have to create a JSON using Flight::json.
I have an array, called $data, that contains some elements like
$data[] = array('id'=>$temp,'type'=>'remote','url'=>$path);
where $id and $path have different values like this:
[id] => http://desktop-pqb3a65:8080/marmotta/resource/22086372-476f-4974-b538-64019ab678b3
[url] => D:\Software\Marmotta\marmotta-home\resources\1d\4d\ea\1d4dea13-f8e6-4cf0-b96a-f88b08efda2b
When I try to convert that in a JSON using:
Flight::json($data);
my PHP page returns me this format instead:
{"id":"http:\/\/desktop-pqb3a65:8080\/marmotta\/resource\/22086372-476f-4974-b538-64019ab678b3","type":"remote","url":"D:\\Software\\Marmotta\\marmotta-home\\resources\\1d\\4d\\ea\\1d4dea13-f8e6-4cf0-b96a-f88b08efda2b"}
I read the documentation and I tried to convert using another function:
Flight::json($data, $code = 200, $encode = false, $charset = 'utf-8');
but it return an error like:
500 Internal Server Error
Array to string conversion (8)
So can you help me to convert $data without this type of error? I must use Flight to convert my array.
Thanks all for help!
EDIT
I solve my problem creating a function like this:
Flight::map('jsonc', function($obj, $status = 200) {
Flight::response()
->status($status)
->header('Content-Type', 'application/javascript')
->write(utf8_decode(json_encode($obj, JSON_UNESCAPED_SLASHES)))
->send();
});
If you want unescaped slashes I believe you can pass that as the 5th function parameter named $option, see source.
public function _json(
$data,
$code = 200,
$encode = true,
$charset = 'utf-8',
$option = 0
)
Edit
Thanks for all the input on this, I did find error in my question so modifying now. Sorry for that.
I am trying to figure out how to return the last object in the JSON string I have rendered. The two functions I am working with:
public function revision($return = false)
{
$id = $this->input->post('galleryID');
$data = array('revision_count' => $this->revision->count_revision($id) );
if($return){
return json_encode($data);
}
else {
echo json_encode($data);
}
}
public function last_revision()
{
$allRevisions = json_decode($this->revision(),true);
return end($allRevisions);
}
The issue is that end() returns error stating that 1st parameter should be array.
Thanks for any help on this.
It is important to note here that json_decode returns an instance of stdClass by default. Try using json_decode($jsonstring, true) to return the JSON as a PHP associative array.
However, You haven't included what the $this->revision() method does. Could you possibly show that portion of the code, since that is the function you are getting a return value from?
Edit:
Alright, after we saw the right function in your code, here are a couple of things I would like to say:
You have added a $return parameter to your revision method, but you aren't using it when you need to. You should change $this->revision() to $this->revision(true) in your last_revision method.
If you're going to return data from the revision() method, there's not much of a point in json_encodeing it, just to json_decode the result. Just pass back the raw data array.
Once you have changed both of these things, this should work:
$allRevisions = $this->revision(true); return end($allRevisions['revision_count']);
You can change the edit_function() to:
public function edit_revision($return = false)
{
$galleryID = $this->input->post('galleryID');
$revisionID = $this->input->post('revisionID');
$data = array('revision_images' => $this->revision->get($galleryID, $revisionID) );
if($return)
return json_encode($data);
else
echo json_encode($data);
}
and then:
public function last_revision(true)
{
$allRevisions = json_decode($this->revision());
return end($allRevisions);
}
Maybe you need convert that json output to an php array (json_decode() function), then you could get the last item with array_pop() function:
https://php.net/array_pop
I get a strange error using json_decode(). It decode correctly the data (I saw it using print_r), but when I try to access to info inside the array I get:
Fatal error: Cannot use object of type stdClass as array in
C:\Users\Dail\software\abs.php on line 108
I only tried to do: $result['context'] where $result has the data returned by json_decode()
How can I read values inside this array?
Use the second parameter of json_decode to make it return an array:
$result = json_decode($data, true);
The function json_decode() returns an object by default.
You can access the data like this:
var_dump($result->context);
If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:
var_dump($result->{'from-date'});
If you want an array you can do something like this:
$result = json_decode($json, true);
Or cast the object to an array:
$result = (array) json_decode($json);
You must access it using -> since its an object.
Change your code from:
$result['context'];
To:
$result->context;
Use true as the second parameter to json_decode. This will decode the json into an associative array instead of stdObject instances:
$my_array = json_decode($my_json, true);
See the documentation for more details.
Have same problem today, solved like this:
If you call json_decode($somestring) you will get an Object and you need to access like $object->key , but if u call json_decode($somestring, true) you will get an dictionary and can access like $array['key']
It's not an array, it's an object of type stdClass.
You can access it like this:
echo $oResult->context;
More info here: What is stdClass in PHP?
As the Php Manual say,
print_r — Prints human-readable information about a variable
When we use json_decode();, we get an object of type stdClass as return type.
The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). I found 2 ways to deal with this.
Cast the object to array.
This can be achieved as follows.
$a = (array)$object;
By accessing the key of the Object
As mentioned earlier, when you use json_decode(); function, it returns an Object of stdClass. you can access the elements of the object with the help of -> Operator.
$value = $object->key;
One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.
$value = $object->key1->key2->key3...;
Their are other options to print_r() as well, like var_dump(); and var_export();
P.S : Also, If you set the second parameter of the json_decode(); to true, it will automatically convert the object to an array();
Here are some references:
http://php.net/manual/en/function.print-r.php
http://php.net/manual/en/function.var-dump.php
http://php.net/manual/en/function.var-export.php
Try something like this one!
Instead of getting the context like:(this works for getting array index's)
$result['context']
try (this work for getting objects)
$result->context
Other Example is: (if $result has multiple data values)
Array
(
[0] => stdClass Object
(
[id] => 15
[name] => 1 Pc Meal
[context] => 5
[restaurant_id] => 2
[items] =>
[details] => 1 Thigh (or 2 Drums) along with Taters
[nutrition_fact] => {"":""}
[servings] => menu
[availability] => 1
[has_discount] => {"menu":0}
[price] => {"menu":"8.03"}
[discounted_price] => {"menu":""}
[thumbnail] => YPenWSkFZm2BrJT4637o.jpg
[slug] => 1-pc-meal
[created_at] => 1612290600
[updated_at] => 1612463400
)
)
Then try this:
foreach($result as $results)
{
$results->context;
}
To get an array as result from a json string you should set second param as boolean true.
$result = json_decode($json_string, true);
$context = $result['context'];
Otherwise $result will be an std object. but you can access values as object.
$result = json_decode($json_string);
$context = $result->context;
Sometimes when working with API you simply want to keep an object an object. To access the object that has nested objects you could do the following:
We will assume when you print_r the object you might see this:
print_r($response);
stdClass object
(
[status] => success
[message] => Some message from the data
[0] => stdClass object
(
[first] => Robert
[last] => Saylor
[title] => Symfony Developer
)
[1] => stdClass object
(
[country] => USA
)
)
To access the first part of the object:
print $response->{'status'};
And that would output "success"
Now let's key the other parts:
$first = $response->{0}->{'first'};
print "First name: {$first}<br>";
The expected output would be "Robert" with a line break.
You can also re-assign part of the object to another object.
$contact = $response->{0};
print "First Name: " . $contact->{'first'} . "<br>";
The expected output would be "Robert" with a line break.
To access the next key "1" the process is the same.
print "Country: " . $response->{1}->{'country'} . "<br>";
The expected output would be "USA"
Hopefully this will help you understand objects and why we want to keep an object an object. You should not need to convert an object to an array to access its properties.
You can convert stdClass object to array like:
$array = (array)$stdClass;
stdClsss to array
When you try to access it as $result['context'], you treating it as an array, the error it's telling you that you are actually dealing with an object, then you should access it as $result->context
Here is the function signature:
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
When param is false, which is default, it will return an appropriate php type. You fetch the value of that type using object.method paradigm.
When param is true, it will return associative arrays.
It will return NULL on error.
If you want to fetch value through array, set assoc to true.
I got this error out of the blue because my facebook login suddently stopped working (I had also changed hosts) and throwed this error. The fix is really easy
The issue was in this code
$response = (new FacebookRequest(
FacebookSession::newAppSession($this->appId, $this->appSecret),
'GET',
'/oauth/access_token',
$params
))->execute()->getResponse(true);
if (isset($response['access_token'])) { <---- this line gave error
return new FacebookSession($response['access_token']);
}
Basically isset() function expect an array but instead it find an object. The simple solution is to convert PHP object to array using (array) quantifier. The following is the fixed code.
$response = (array) (new FacebookRequest(
FacebookSession::newAppSession($this->appId, $this->appSecret),
'GET',
'/oauth/access_token',
$params
))->execute()->getResponse(true);
Note the use off array() quantifier in first line.
instead of using the brackets use the object operator for example my array based on database object is created like this in a class called DB:
class DB {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct() {
try{
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db') , Config::get('mysql/username') ,Config::get('mysql/password') );
} catch(PDOException $e) {
$this->_error = true;
$newsMessage = 'Sorry. Database is off line';
$pagetitle = 'Teknikal Tim - Database Error';
$pagedescription = 'Teknikal Tim Database Error page';
include_once 'dbdown.html.php';
exit;
}
$headerinc = 'header.html.php';
}
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param){
$this->_query->bindValue($x, $param);
$x++;
}
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else{
$this->_error = true;
}
return $this;
}
public function action($action, $table, $where = array()) {
if(count($where) ===3) {
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} = ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
public function results() {
return $this->_results;
}
public function first() {
return $this->_results[0];
}
public function count() {
return $this->_count;
}
}
to access the information I use this code on the controller script:
<?php
$pagetitle = 'Teknikal Tim - Service Call Reservation';
$pagedescription = 'Teknikal Tim Sevice Call Reservation Page';
require_once $_SERVER['DOCUMENT_ROOT'] .'/core/init.php';
$newsMessage = 'temp message';
$servicecallsdb = DB::getInstance()->get('tt_service_calls', array('UserID',
'=','$_SESSION['UserID']));
if(!$servicecallsdb) {
// $servicecalls[] = array('ID'=>'','ServiceCallDescription'=>'No Service Calls');
} else {
$servicecalls = $servicecallsdb->results();
}
include 'servicecalls.html.php';
?>
then to display the information I check to see if servicecalls has been set and has a count greater than 0 remember it's not an array I am referencing so I access the records with the object operator "->" like this:
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
<!--Main content-->
<div id="mainholder"> <!-- div so that page footer can have a minum height from the
header -->
<h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
<br>
<br>
<article>
<h2></h2>
</article>
<?php
if (isset($servicecalls)) {
if (count ($servicecalls) > 0){
foreach ($servicecalls as $servicecall) {
echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
.$servicecall->ServiceCallDescription .'</a>';
}
}else echo 'No service Calls';
}
?>
Raise New Service Call
</div> <!-- Main content end-->
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>
It is most likely when it tries to access the data with the generic bracket array accessor and not an object operator. Always ensure the variable type is before accessing the data.
While decoding the JSON, the response will be generated as stdObject instances
Rather than calling $result['context'];, access it by $result->context;
If it needs to call as an array itself, decode the JSON by passing the second parameter as true like
json_decode($jsonData, true);
Change it for
$results->fetch_array()