Coverting String to PHP Associative Array - php

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.

Related

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

Save and retrieve a php array through mysql

I want to save a long array in mysql database and when i read that array back from mysql database, i want that array back in proper array format. Is this possible?
Other threads are suggesting to convert array into string using serialise and explode. But will they help me to get proper array back?
Thanks.
You can try it yourself.
As an alternative to serailize(), you can use json_encode().
sidenote: reverse of serialize() is unserialize(), not explode().
sidenote: reverse of json_encode() is json_decode().
sidenote: Very worthwhile to read: Discussion of json_encode() vs serialize()
Usually, serialize and unserialize functions are used for such purposes.
With serialize you can convert your array to a string and than get an array by applying unserialize on string you get from serialize.
And about explode: you may use it too, but than you will need to use implode function to serialize array. But it will work with simplest one dimensional arrays only:
implode(",", array("val1", "val2", "val3")) = "val1,val2,val3"

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

Read this using php

Is there any way to read/write the following data using php? And what is this called?
a:1:{s:13:"administrator";s:1:"1";}
Looks like data serialized by the PHP serialize() function. You can read it using unserialize().
$serialized = 'a:1:{s:13:"administrator";s:1:"1";}';
$data = unserialize($serialized);
print_r($data);
try using unserialize() if that is a serialized data then that should unserialize it.
Yep... That's serialized data. You can unserialize it for pure chechup purposes here - http://unserialize.net/serialize.
Other than that use unserialize() in your PHP code.

Categories