How is PHP object called that has colons and brackets? [duplicate] - php

This question already has answers here:
What is the type this string? a:1:{s:2:"en";}
(3 answers)
Closed 8 years ago.
What kind of string is this? How can I unserialize it and get the array out of it?
a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}

This is a serialized string. You can unserialize it with this function: unserialize(), like this:
$str = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
print_r(unserialize($str));
Output:
Array ( [0] => Abogado [1] => Notario )
Side Note:
A quote from the manual:
Warning:
FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. It is possible to catch this special case by comparing str with serialize(false) or by catching the issued E_NOTICE.
Warning:
Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.

I used this:
$argument = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
$arr = unserialize(urldecode($argument));
print_r($arr);

Related

trying to get value from array by json_encode Illegal string offset [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Here is what I have tried:
$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_encode($arrayToJson);
print_r($arrayToJson['docnum']);
The result is: "
You've made a mistake with how you are trying to convert the object to an associative array in your PHP.
You should be using json_decode to decode the string to an object. Additionally, you want it to be an associative array so must specify TRUE for the second parameter of json_decode:
$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_decode($arrayToJson, TRUE);
print_r($arrayToJson['docnum']);
OUTPUT:
3006
For your reference, the documentation for json_decode is here.
First of all, you want to decode your JSON, not encode it a second time.
And secondly, your JSON contains an object, so you either need to use $arrayToJson->docnum here to access the property, or you need to decode it with the second parameter of json_decode set to true, so that the result will be converted into an array.

What kind of string is this? How do I unserialize this string? [duplicate]

This question already has answers here:
What is the type this string? a:1:{s:2:"en";}
(3 answers)
Closed 8 years ago.
What kind of string is this? How can I unserialize it and get the array out of it?
a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}
This is a serialized string. You can unserialize it with this function: unserialize(), like this:
$str = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
print_r(unserialize($str));
Output:
Array ( [0] => Abogado [1] => Notario )
Side Note:
A quote from the manual:
Warning:
FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. It is possible to catch this special case by comparing str with serialize(false) or by catching the issued E_NOTICE.
Warning:
Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.
I used this:
$argument = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
$arr = unserialize(urldecode($argument));
print_r($arr);

Can PHP parse this data? [duplicate]

This question already has answers here:
How to use php serialize() and unserialize()
(10 answers)
Closed 8 years ago.
Data like:
a:5:{i:0;s:0:"";i:1;s:0:"";s:7:"message";s:0:"";s:5:"medal";N;s:5:"users";s:0:"";}
and what's this datatype?
The datatype is a non-standard protocol which exists only within the php-src scripting language.
To properly create a PHP value from a stored representation you would use the unserialize()` function provided by the SPL.
unserialize('a:2:{s:9:"usergroup";s:0:"";s:6:"verify";s:0:"";}');
unserialize() takes a single serialized variable and converts it back into a PHP value.
Couldn't have been that hard if you tried
print_r(unserialize('a:2:{s:9:"usergroup";s:0:"";s:6:"verify";s:0:"";}'));
Output:
Array
(
[usergroup] =>
[verify] =>
)

PHP array value into second array [duplicate]

This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 9 years ago.
I've been searching around but i'm failing to find an answer to something that seems it should be simple to fix!
I'm reading products from XML and putting their data into an array on a loop, the array is called $res.
Now, i need to put a value from $res into another array for loading to the DB (magento SOAP API). But when i do this i do not get a string value i wxpect, instead i get an the first array inside the second.
Here is the problem line:
$fieldDateData = array('rts_date'=>$res[0]->BackInStockDate1);
I've tried a few different things, none have worked. I thought it would be simply enough to do this:
$data = $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
But sadly not, i'm unsure as to why?
Thanks,
EDIT:
This is an example of the output
Array
(
[rts_date] => SimpleXMLElement Object
(
[0] => 28/06/13
)
)
Try
$data = (string)$res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
You need to cast the value you are setting as a string:
$data = (string) $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);

Store array in cookie [duplicate]

This question already has answers here:
Storing PHP arrays in cookies
(9 answers)
Closed 1 year ago.
I am converting the array into cookie by php serialize function
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", serialize($Promotedcart), time()+604800,'/');
And when the cookie is created then i am using the unserialize php function.
print_r(unserialize($_COOKIE['Promotedcart']));
it does not work.
When I print_R($_COOKIE) then it show me the value.
Cookies separated by semicolon. Serialized strings with arrays contain them inside. Maybe this is a problem. You can use base64 to avoid all possible escape issues.
You can use json_encode, json_decode functions to achieve this as an alternative.
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", json_encode($Promotedcart), time()+604800,'/');
$result = json_decode($_COOKIE['Promotedcart'], true);
print_r($result);
Give it a try, this should work.

Categories