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
Related
I've come across PHP code with the a check which tests if a POST value is an instanceof a class:
if ($_POST['something'] instanceof SomeClass) {
// do something
}
This seems odd to me, because I wouldn't think that the check can ever be true. A POST value is a string after all, and a string isn't an instance of a class.
I tried passing the serialized version of an instance (O:9:"SomeClass":0:{}), but that doesn't work (which makes sense, as it's still a string, not an object).
Am I correct in thinking that this check can never be true? Or am I missing something here?
I think this is not a simple question. I think theoretically "yes".
As a $_POST is a array of variables, and there is no limitation what the elements of a array can be, you can for example create a array of objects.
$array[] = new stdClass;
$array[0]->variable = value;
etc ...
You see the code author is checking if $_POST['something'], that is a element of the array, and that easily can be a object, is a instance of a class.
Now I have not tested it, but theoretically one could put a object of a class in a array and send it via $_POST nicely encoded.
I have an array object below
33 => 'a:2:{
s :8:"latitude";
s:10:"39.3600586";
s:9:"longitude";
s:18:"-84.30993899999999";
}'
And here is the variable I'm using to get the value of that object
$events_location = $entries[1]['33'];
Is there a way to get just the value of either the latitude or longitude instead of everything in the single quotes?
Thanks!
What you have here is a serialized string. Unserialize it to access the key in the array:
$events_location = unserialize($entries[1]['33']);
echo $events_location['longitude'];
This should be a comment but its a bit long.
The string you have shown us looks vaguely like part of a serialized php entity. But it's not. If you try to unserialize this you'll get an error. The underlying data appears to be a coordinate - but even ignoring the syntactic errors, the semantics of the structure are wrong.
Please check the source you transcribed this from. If you have not copied the original content here please amend your question.
If you have transcibed it correctly then go speak to whoever supplied you with this data and ask them to fix it.
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.
Say you have a method that receives an array as a parameter. That method does some manipulation with that array and then outputs a different array.
public function createArrayForX(array $myArray){
return $modifiedArray;
}
In order for me to test that the output has some values I need to create some sample cases for my input. It is a tedious work to create arrays manually, especially big ones.
public function testCreateArrayForX(){
$myArray = []; // **how do I easily create myArray here**
$this->assertContains("String that I want", $this->sampleClass->createArrayForX($myArray));
}
I tried something like var_dumping my array in the program, copying the result from var_dump and transforming it back to array (Convert var_dump of array back to array variable) and then edit it to make different tests. Is there a simple way or am I doing things wrong if I need this?
So my question is how do you easily create a skeleton for you sample input that you can then change to test different scenarios?
print(var_export($myArray, true));
in the program gave me what I need.
I am having trouble understanding the concept of serialize/unserialize in PHP.
Assume I have a very simple PHP object (class someObject) and after setting the attributes of that object I want to serialize it:
So I call: serialize($someObject);
I want to transfer this serialized object into another php skript via a html form so I set it as a hidden value:
<input type="hidden" name="someObject" value="<? print $someObject; ?>"
In the next php script I want to use unserialize to get my object back and transfer it e.g. to a databae.
$unserialize = unserialize($_POST['someObject'])
But this always returns BOOL(false) - so what am I missing here?
Thanks for your help!
A serialized string looks like this:
O:1:"a":1:{s:3:"foo";s:3:"100";}
You have tourlencode/urldecode the serialized string to prevent any characters in the serialized representation from breaking your markup. Have a look at your page source. The first quote likely ended your HTML value attribute. So you got something like:
<input ... value="O:1:"a":1:{s:3:"foo";s:3:"100";}">
So your $_POST will never contain the full serialized string, but only O:1:
If this is not the issue, make sure you got a serialized string from the object in the first place. Also please be aware that there some objects cannot be serialized or have modified behavior when (un)serialized. Please refer to the Notes in PHP Manual for serialize for details.
If you dont need to send objects across different servers running PHP, consider persisting them in a Session instead. It's easier, less error prone and more secure because the object cannot be tampered with while in transit.
You need to have the class defined in your second script before you unserialize() the object