php parse print_r output - php

When I print an object using print_r, is there any specific utility function in php to convert the output to an object?
There seems to be enough output to reconstruct the object, but I couldn't find the exact functions to accomplish this.

There is no automatic method to change print_r value back to an object. You are also missing important information like what type is stored at a key (string or number or ..)
You could use var_dump to get more detailed output that can be changed back to an object. But still this does not mean there is an automatic function for it.
Last you could use var_export ( http://php.net/manual/en/function.var-export.php ) to get valid PHP output you can use.
You could also use http://php.net/manual/en/function.serialize.php to pass objects around.

Related

How to parse stdClass object in php

Using var_dump from an API call give me the output like this
How to put them into an array? thanks
You can use json_encode to convert it to a json. After which you can use json_decode with the second parameter set to true which will return an array instead of an StdObject.
If the api you're calling simply returns a json itself then you can go ahead and use json_decode directly with the second argument set to true to yield the same result.
There are other ways, but this is the simplest.
You can check out other questions, like this one or this one.

how can i get back an array written in a text file with print_r [duplicate]

When I print an object using print_r, is there any specific utility function in php to convert the output to an object?
There seems to be enough output to reconstruct the object, but I couldn't find the exact functions to accomplish this.
There is no automatic method to change print_r value back to an object. You are also missing important information like what type is stored at a key (string or number or ..)
You could use var_dump to get more detailed output that can be changed back to an object. But still this does not mean there is an automatic function for it.
Last you could use var_export ( http://php.net/manual/en/function.var-export.php ) to get valid PHP output you can use.
You could also use http://php.net/manual/en/function.serialize.php to pass objects around.

Convert key-value JSON to object and refer individual values directly

I am making a web app. In one part of it, I have JS send a string(in json format) to PHP.
The value php receives is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
Now, this is saved in a variable $meta. The problem I am facing is, as to how do I convert this string into an object and reference each individual entry separately.
I have tried json_decode and json_encode
and then I have referenced each variable using $meta.["date"] and $meta.date but nothing seams to work. I am getting just { as the output.
What's the correct way to do this?
$str = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$obj = json_decode($str);
echo $obj->date;
// 24-03-2014
Usually a $my_obj = json_decode($_POST['jsonstring'], 1); (true supply means it'll be returned as an assoviative array) should be the way to go. If I were you I'd probably try a var_dump($my_obj); to see what actually comes through. If it doesn't work you'll want to make sure that you correctly submit a valid json string, e.g. JSON.stringify();
You should check out the PHP doc page for json_decode here.
By default, unless you pass true as the second parameter for json_decode, the function call will return an object, which you can access the members of by using:
$meta->date
The arrow operator will allow you to access object values, not the square brackets or a dot.

how to convert a text into an associative array?

Hi this is the code i am following in my project.
$reports = $this->curl->simple_get('url');
echo is_array($reports)?"Is Array":"Not Array";exit;
It's giving Not Array as output.
I want to convert that into associative array.
The data you are getting is probably not an array, but a string containing an array structure, e.g. output by print_r(). This kind of data will not automatically be converted back into a PHP array.
To use this you can use a similar solution as brought out here:
Create variable from print_r output
it describes the print_r_reverse function that's brought out in php.net page.
how ever - this is kind of an ugly hack. I would suggest to change the page content and use json_encode() in the "url" page, and parse the content using json_decode()

json_encode "not working" properly?

Of course, the subject is sort of tricky... I assume json_encode php function works perfectly.
But there must be something wrong on what I am doing.
I have a variable in my PHP program that actually contains data. Sort of structured data, as it is an object variable with all its attributes set properly.
But, magically, when I apply json_encode to that variable, it just doesn't "fills" the json object.
See the case below:
As you can see... the variable $test contains data.
Now, putting the focus on the whole json_encoder($test) thing, it just doesn't fills the "test" {} part of the json object.
It's definitely me doing something wrong. But... what?
Thanks a lot in advance.
Pedro
json_encode only works with public properties, it cannot take the private or protected attributes of an object and form a JSON representation without any help.
To serialize an object with all of its properties, you need to manually implement a function to do so. The official PHP documentation has a good example on how to do this here

Categories