cast string to JSON - php

Is there any way to make PHP look at a string as a JSON?
I have a string, in a JSON format of course, and I want to perform actions on it like it was an array. However I don't want to use CJSON::decode because it takes a long time, Is there a way?
Example for the string:
{"myArray":[{"key1":"val1","key2":"val2","key3":"val3","key4":"val4"},
{"key1":"val2_1","key2":"val2_2","key3":"val2_3","key4":"val2_4"}]}

Not sure what CJSON::decode is but you have to decode the string so you can use the built in function json_decode($str);

how about json_decode() ?
I don't think there is faster than that
$string = '{"myArray":[{"key1":"val1","key2":"val2","key3":"val3","key4":"val4"}, {"key1":"val2_1","key2":"val2_2","key3":"val2_3","key4":"val2_4"}]}';
$array = json_decode($string);

Related

Best way to json encode an array that has a json encoded value already? PHP

I have an array with multiple items in it one of which is an already encoded json string. I'm wanting to json encode the whole array but in doing so it re-json_encodes the json and adds slashes to it. The only way I've found to fix this is to json_decode the value and then encode the whole array. I feel like this is a waste of resources though and I feel like there has to be a better way. Is doing it that way the best possible way?
Here's a sample of the array I'm trying to json_encode.
$arr = array();
$arr["var1"] = '{"test":"test"}';
$arr["var2"] = 'foo';
$arr["var3"] = 'bar';
If I don't decode the var1 first and I just encode the whole array I get a result like so
{"var1":"{\"test\":\"test\"}","var2":"foo","var3":"bar"}
Notice the slashes in the json object.
json_encode() returns a string containing the json representation of a value.
In the example, a php string is passed as one element of the array '{"test":"test"}', thus json_encode() is encoding it appropriately into json format, with escaped quotes "{\"test\":\"test\"}".
If decoding nested json is a very resource heavy task, a workaround is to use a placeholder instead of the value, {"var1":"PLACEHOLDER","var2":"foo","var3":"bar"}, and then using str_replace() to replace it.
However, simply decoding it as you described is probably a cleaner solution, if its not resource heavy.

Converting string-array to an array of strings

I am struggling with converting a string into an array:
["Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"]
and I want to convert it into an array of values inside quotes "".
Tried (un)serialize(), parse_str(). They don't cope with it.
Since nobody else is going to post it, that looks like JSON:
$array = json_decode($string, true);
print_r($array);
The true parameter isn't needed for this JSON, but if you want to make sure you always get an array and not an object regardless of the JSON then use it.
It would be easiest to use json_decode:
json_decode('["Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"]', true)
But if for some reason you are not parsing is as json, you should be able to use explode:
explode(',', '"Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"');
If you need to deal with the brackets, you can trim them from the string with something like this prior to exploding:
$string = trim('["Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"]', '[]');

Coverting String to PHP Associative Array

How can i convert this string
$str = "array('3'=>'wwm','1'=>'wom')";
to real php associative array...
It's simple but REALLY INSECURE
$str = "array('3'=>'www.tension.com','1'=>'www.seomeuo.com','requestedBy'=>'1')";
eval("\$array = $str;");
You never should use this approach, there another ways to do it like: serialize() and unserialize()
You can use the eval() function for that:
$str = "array('3'=>'wwm','1'=>'wom')";
eval("\$a=$str;");
var_dump($a);
However using eval() in your code is considered to be risky and you should not use it. Try to use serialize(), unserialize() instead.
First of all. Do not use eval. It is Evil!
http://af-design.com/blog/2010/10/20/phps-eval-is-evil/
Secondly. The simple solution would not to be using this string but simply to use "serialize" when you put it in the DB and unserialize when you pull it out. You are storing a very unusual format.

Parse a json(?) string using php

I have a string, more specifically, this one:
a:16:{s:9:"pseudonym";O:16:"SimpleXMLElement":0:{}s:14:"parallel_title";O:16:"SimpleXMLElement":0:{}s:9:"title_var";O:16:"SimpleXMLElement":0:{}s:6:"series";O:16:"SimpleXMLElement":0:{}s:9:"vol_title";O:16:"SimpleXMLElement":0:{}s:9:"reference";O:16:"SimpleXMLElement":0:{}s:10:"bound_with";O:16:"SimpleXMLElement":0:{}s:15:"general_remarks";O:16:"SimpleXMLElement":0:{}s:6:"copies";O:16:"SimpleXMLElement":1:{i:0;s:1:"1";}s:11:"remarks_BPH";O:16:"SimpleXMLElement":0:{}s:3:"ICN";O:16:"SimpleXMLElement":1:{i:0;s:4:"neen";}s:10:"provenance";O:16:"SimpleXMLElement":0:{}s:7:"binding";O:16:"SimpleXMLElement":0:{}s:10:"size_hxwxd";O:16:"SimpleXMLElement":0:{}s:6:"BookID";O:16:"SimpleXMLElement":1:{i:0;s:4:"6271";}s:5:"repro";O:16:"SimpleXMLElement":0:{}}
Is it possible to parse this string somehow? I need to display the keys and values in a list. I tried to use json_decode but it doesn't return anything, even with the second parameter set to true:
json_decode($string,true);
It's not JSON, it's serialized PHP. Use unserialize().
It's serialize object
Read more on PHP website

PHP text-extraction from string

I have the following string: a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}
What I need to do is extract number 48 from it, in this case. This number could have any number of digits. How would I go about doing this?
It looks like you are facing a serialized strings. So, instead of trying to get that number using regular expression or any other string manipulation methods, try this:
$myVar = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
$myNumber = $myVar['userid'];
Learn about PHP serialization here:
http://php.net/manual/en/function.serialize.php
http://php.net/manual/en/function.unserialize.php
What exactly are you trying to achieve? That string looks like a serialize()d one, so your best bet would be to unserialize() it
It looks like serialized string.
$data = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
print_r($data['userid']);
Looks like that's a serialized associative array. You just need to use unserialize() to turn it back from a string into an array.
<?php
$arr = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
echo $arr['userid'];
?>
The string I see is serialized array in PHP
To unserialize array do this
$obj = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
echo $obj['userid'];
I have unserialized array then access array param by name

Categories