Good evening,
This is probably a stupid question but I've been fiddling with this for a while now. I have a string coming from an AJAX call. To inspect the actual string that gets sent to my PHP from some JavaScript I put it into the result and spit it out in the front end. The string looks like this:
count: "[{\"cartKey\":\"d9d4f495e875a2e075a1a4a6e1b9770f\",\"qty\":\"3\"},
{\"cartKey\":\"67c6a1e7ce56d3d6fa748ab6d9af3fd7\",\"qty\":\"2\"},
{\"cartKey\":\"f7177163c833dff4b38fc8d2872f1ec6\",\"qty\":\"32\"}]"
So! My problem is getting this to be an actual PHP array. If I do this:
$result['count'] = json_decode($updates, true);
inside my PHP, the result is 0.
Ignore the count name on the result. I'm just trying to turn the above string into an array of objects I can use in PHP rather than a JSON string.
Thanks in advance!
I'm a birk.
The actual value of $updates was null, I think.
I have just solved my problem by not calling JSON.stringify on the data sent from the AJAX call.
The value now comes through and is actually decoded as if by magic within the PHP script.
Thanks for taking a look.
Ash
I need to extract the string "Post Title" from this array. I have no idea how to reach it.
a:1:
{ i:0;
a:5:
{
s:4:"data";s:9:"Post Title";
s:7:"attribs";a:0:{}
s:8:"xml_base";s:0:"";
s:17:"xml_base_explicit";b:0;
s:8:"xml_lang";s:0:"";
}
}
As the comments say, you have a serialized array. But there are two potential problems with using unserialize.
You should not use unserialize on untrusted data. There's a big warning in the documentation. If you don't trust the data fully, I suggest a safer alternative like _safe_unserialize used by myBB. You can find it on github.
Second your string looks corrupted in one place. It should be s:10:"Post Title";. That means that unserialize/safe_unserialize will throw errors. To fix that, take a look at the first two answers of this question.
After you did all that, and stored the unserialized data in - let's say $arr - you can access the post title via: $array[0]['data'].
So... I need to save a large-ish amount of data from a platform with an excruciatingly limited amount of memory.
Because of this, I'm basically storing the data on my webserver, using a php script to just write JSON to a flat file, because I'm lazy af.
I could go to the trouble of having it store the data in my mysql server, but frankly the flat file thing should have been trivial, but I've run up against a problem. There are several quick and dirty workarounds that would fix it, but I've been trying to fix it the "right" way (I know, I know, the right way would be to just store the data in mysql, but I actually need to be able to take the json file this produces and send it back to the platform that needs the data (In a ridiculously roundabout fashion), so it made sense to just have the php save it as a flat file in the first place. And It's already working, aside from this one issue, so I hate to reimpliment.
See... Because of the low memory on the platform I'm sending the json to my server from... I'm sending things one field at a time. Each call to the php script is only setting ONE field.
So basically what I'm doing is loading the file from disk if it exists, and running it through json_decode to get my storage object, and then the php file gets a key argument and a value argument, and if the key is something like "object1,object2", it explodes that, gets the length of the resulting array, and then stores the value in $data->$key[0]->$key[1].
Then it's saved back to disk with fwrite($file, json_encode($data));
This is all working perfectly. Except when $value is a simple string. If it's an array, it works perfectly. If it's a number, it works fine. If it's a string, I get null from json_decode. I have tried every way I can think of to force quotes on to the ends of the $value variable in the hopes of getting json_decode to recognize it. Nothing works.
I've tried setting $data->$key[0]->$key[1] = $value in cases where value is a string, and not an array or number. No dice, php just complains that I'm trying to set an object that doesn't exist. It's fine if I'm using the output of json_decode to set the field, but it simply will not accept a string on its own.
So I have no idea.
Does anyone know how I can either get json_decode to not choke on a string that's just a string, or add a new field to an existing php object without using the output of json_decode?
I'm sure there's something obvious I'm missing. It should be clear I'm no php guru. I've never really used arrays and objects in php, so their vagaries are not something I'm familiar with.
Solutions I'm already aware of, but would prefer to avoid, are: I could have the platform that's sending the post requests wrap single, non-numeric values with square braces, creating a single item array, but this shouldn't be necessary, as far as I'm aware, so doing this bothers me (And ends up costing me something like half a kilobyte of storage that shouldn't need to be used).
I could also change some of my json from objects to arrays in order to get php to let me add items more readily, but it seems like there should be a solution that doesn't require that, so I'd really prefer not to...
I skim through your post.
And I know this works for StdClass :
$yourClass->newField = $string;
Is this what you wanted ?
OK so... ultimately, as succinctly as possible, the problem was this:
Assuming we have this JSON in $data:
{
"key1":
{
"key2":["somedata","someotherdata"]
}
}
And we want it to be:
{
"key1":
{
"key2":["somedata","someotherdata"],
"key3":"key3data"
}
}
The php script has received "key=key1,key3&value=key3data" as its post data, and is initialized thusly:
$key = $_POST["key"];
$key = explode($key,",");
$value = $_POST["value"];
...which provides us with an array ($key) representing the nested json key we want to set as a field, and a variable ($value) holding the value we want to set it to.
Approach #1:
$data->$key[0]->$key[1] = json_decode($value);
...fails. It creates this JSON when we re-encode $data:
{
"key1":
{
"key2":["somedata","someotherdata"],
"key3":null
}
}
Approach #2:
$data->$key[0]->$key[1] = $value;
...also fails. It fails to insert the field into $data at all.
But then I realized... the problem with #2 is that it won't let me set the nonexistent field, and the problem with approach #1 is that it sets the field wrong.
So all I have to do is brute force it thusly:
$data->$key[0]->$key[1] = json_decode($value);
if (json_decode($value) == NULL)
{
$data->$key[0]->$key[1] = $value;
}
This works! Since Approach #1 has created the field (Albeit with the incorrect value), PHP now allows me to set the value of that field without complaint.
It's a very brute force sort of means of fixing the problem, and I'm sure there are better ones, if I understood PHP objects better. But this works, so at least I have my code working.
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
Hey can anyone tell me what datatype this is? it wont parse as i've stripped out sensitive data. Am I correct in thinking its json?
a:7:{s:12:"competitions";a:893:{i:1;s:10:"Ersie";i:5;s:19:"General
News"1510126584;s:0:"";i:1019;s:0:"";i:8284;s:0:"";i:191016;s:0:"";i:284;s:0:"";i:91019;s:0:"";i:81863;s:0:"";i:1563;s:0:"";i:710138;s:0:"";i:101333;s:0:"";i:33430;s:0:"";i:10224;s:0:"";i:10430;s:0:"";i:13430;s:0:"";i:375;s:0:"";i:72107;s:0:"";i:11372;s:0:"";i:181372;s:0:"";i:1885;s:0:"";i:107155;s:0:"";i:10284;s:0:"";i:8206;s:0:"";i:8101316;s:0:"";i:1913;s:0:"";i:206;s:0:"";i:772138;s:0:"";i:72284;s:0:"";i:1672155;s:0:"";i:7101663;s:0:"";i:891013;s:0:"";i:101843;s:0:"";i:1107;s:0:"";i:1072;s:0:"";i:830;s:0:"";i:7284;s:0:"";i:8101333;s:0:"";i:13372;s:0:"";i:1570;s:0:"";i:2543;s:0:"";i:91316;s:0:"";i:385;s:0:"";i:8385;s:0:"";i:2843;s:0:"";i:695;s:0:"";i:1970;s:0:"";i:1661;s:0:"";i:18206;s:0:"";i:33155;s:0:"";i:787;s:0:"";i:8117;s:0:"";i:1943;s:0:"";i:3043;s:0:"";i:872239;s:0:"";i:872155;s:0:"";i:910104;s:0:"";i:8125;s:0:"";i:239;s:0:"";i:8428;s:0:"";i:1382;s:0:"";i:87293;s:0:"";i:95385;s:0:"";i:11672;s:0:"";i:92572;s:0:"";i:828;s:0:"";i:8239;s:0:"";i:640;s:0:"";i:87155241;s:0:"";i:26155241;s:0:"";i:87158241;s:0:"";i:69158241;s:0:"";i:82543;s:0:"";i:193372;s:0:"";i:90163241;s:0:"";i:163372;s:0:"";i:1016107;s:0:"";i:86372;s:0:"";i:87163241;s:0:"";i:155162241;s:0:"";i:713121;s:0:"";i:2690241;s:0:"";i:895430;s:0:"";i:-403549467;s:0:"";i:2060490689;s:0:"";i:7181924;s:0:"";i:157158241;s:0:"";i:87295;s:0:"";i:71672430;s:0:"";i:1372430;s:0:"";i:9430;s:0:"";i:897;s:0:"";i:242;s:0:"";i:87162241;s:0:"";i:161863;s:0:"";i:1824107;s:0:"";i:26165241;s:0:"";i:7891316;s:0:"";i:81670;s:0:"";i:43107;s:0:"";i:710121;s:0:"";i:172283;s:0:"";i:8790241;s:0:"";i:253372;s:0:"";i:71863;s:0:"";i:26157241;s:0:"";i:8393;s:0:"";i:91824;s:0:"";i:826;s:0:"";i:63640;s:0:"";i:26163241;s:0:"";i:13121;s:0:"";i:82574;s:0:"";i:165241;s:0:"";i:87157241;s:0:"";i:2687241;s:0:"";i:26162241;s:0:"";i:199;s:0:"";i:18910;s:0:"";i:162165241;s:0:"";i:751430;s:0:"";i:16125;s:0:"";i:81893;s:0:"";i:79101316;s:0:"";i:81693;s:0:"";i:8913119;s:0:"";i:7818;s:0:"";i:72223;s:0:"";i:781072;s:0:"";i:972107;s:0:"";i:26241;s:0:"";i:90158241;s:0:"";i:87071;s:0:"";i:816125;s:0:"";i:72155430;s:0:"";i:72109;s:0:"";i:8106;s:0:"";i:181;s:0:"";i:8126;s:0:"";i:897293;s:0:"";i:187;s:0:"";i:101318;s:0:"";i:7166372;s:0:"";i:13216;s:0:"";i:101367;s:0:"";i:91149;s:0:"";i:781633;s:0:"";i:7101363;s:0:"";i:872430;s:0:"";i:1657;s:0:"";i:215;s:0:"";i:1025;s:0:"";i:7136372;s:0:"";i:1314;s:0:"";i:101319;s:0:"";i:1372344;s:0:"";i:1372573;s:0:"";i:1672430;s:0:"";i:872109;s:0:"";i:972430;s:0:"";i:193372430;s:0:"";i:13171;s:0:"";i:172430;s:0:"";i:6372430;s:0:"";i:772430;s:0:"";i:1372155430;s:0:"";i:71372;s:0:"";i:887;s:0:"";i:78933344;s:0:"";i:8131672;s:0:"";i:131672;s:0:"";i:365;s:0:"";i:91172;s:0:"";i:772155;s:0:"";i:972155;s:0:"";i:137072;s:0:"";i:729;s:0:"";i:781672;s:0:"";i:891172;s:0:"";i:924;s:0:"";i:796372;s:0:"";i:94372;s:0:"";i:284385;s:0:"";i:72344;s:0:"";i:71372121;s:0:"";i:78972;s:0:"";i:136372;s:0:"";i:713211;s:0:"";i:17972;s:0:"";i:101872;s:0:"";i:17872;s:0:"";i:84370;s:0:"";i:71633;s:0:"";i:172107;s:0:"";i:833732;s:0:"";i:18101318;s:0:"";i:816155;s:0:"";i:83343;s:0:"";i:43155;s:0:"";i:438;s:0:"";i:104363;s:0:"";i:961;s:0:"";i:843732;s:0:"";i:78107;s:0:"";i:13344;s:0:"";i:72393;s:0:"";i:83363;s:0:"";i:113121;s:0:"";i:863121;s:0:"";i:8131643;s:0:"";i:518;s:0:"";i:1789;s:0:"";i:61107;s:0:"";i:891619;s:0:"";i:43214;s:0:"";i:63121;s:0:"";i:7136430;s:0:"";i:81865;s:0:"";i:728430;s:0:"";s:11:"10245074125";s:0:"";i:63162;s:0:"";i:393;s:0:"";i:89732;s:0:"";i:2633430;s:0:"";i:157283430;s:0:"";}s:8:"lastpost";i:1281041491;s:7:"default";s:19:"General
News";s:11:"autopublish";s:2:"on";s:5:"draft";N;s:6:"poster";s:1:"1";s:6:"suffix";s:0:"";}
This is a serialized PHP array. Essentially, it's just a string that represents a PHP array (the a at the beginning marks it as an array).
You can get the PHP array back out of it by passing the string to the PHP function unserialize().
That is serialized data - not Wordpress specific.
Take a look into serialize(), unserialize() and OOP serialization: __sleep()/__wakeUp().