Converting string-array to an array of strings - php

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('["Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"]', '[]');

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.

cast string to JSON

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);

PHP function json_decode decodes float value with zeros after point as int

I have a JSON string that contains some key with the following value: 123.00. When I use json_decode function I get the decoded string where the previous key equal to 123, not to 123.00. Is there a way to correct decode such values without wrapping into quotes?
This is currently being brought up as a PHP bug:
Bug Report: https://bugs.php.net/bug.php?id=50224
In the future, there may be functionality to pass a flag through the options parameter for stricter typing. For now, however, wrapping it in quotes will have to suffice.
I do not think it is possible!
//convert the json to a string before json_decode
$res = preg_replace( '/next_cursor":(\d+)/', 'next_cursor":"\1"', $json );
number_format($number, 2)
output the number through that?
You can use the JSON_BIGINT_AS_STRING option, for example:
$json = json_decode($input, true, 512, JSON_BIGINT_AS_STRING);
Careful though, this only works with PHP 5.4+!

Get string value of array on remote server

I'm trying to find the content of an array on a remote server. All I can send back currently is strings (due to limitations in the PHP implementation of xmlrpc). Normally, I'd just use var_dump(), but that returns void. I've tried using var_export, but I get XML errors, even when I cast the result to a string.
How can I get the string representation of an array?
Use serialize():
$string = serialize( $array);
Then use unserialize() to get it back into an array:
$array = unserialize( $string);
You can also use json_encode() / json_decode() if you're interested in a JSON formatted string.
$string = json_encode( $array);
$array = json_decode( $string);
You can use PHP serialize, json_encode .... These are alternate methods to encode an object.

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