Jump to content

Unserialize


shaunie

Recommended Posts

Hi,

 

I am trying to get the values of field1 and field20 from the following unserialized output:



stdClass::__set_state(array(
  'id' => 'xxx',
  'products' => 
  stdClass::__set_state(array(
    'product' => 
    stdClass::__set_state(array(              
      'field1' => '123',
      'field2' => '',
      'field3' => 
      stdClass::__set_state(array(
      )),      
      #various other fields        
      'field20' => '456',    
    )),
  )),
))


 

I have tried this code but it doesn't seem to be working, can someone tell me what I am doing wrong here please?

 



$result = unserialize($api->getResponseSerialized());                                
foreach ($result->products->product as $product) {
  $product->field1; //doesnt work!!!                                      
}


 

Link to comment
https://forums.phpfreaks.com/topic/284776-unserialize/
Share on other sites

Never used var_export. Used to the output from print_r   ;D

 

try using

foreach ($result->products->product as $field => $value) {
  echo $field . ' = ' . $value . '<br />';
}

If you want to get each fields value manually you'd use

$product = $result->products->product;

echo $product->field1;
//... etc ...
echo $product->field20;
Link to comment
https://forums.phpfreaks.com/topic/284776-unserialize/#findComment-1462387
Share on other sites

Thank you for your reply, 

 

the problem seems to be the after after field3

foreach ($result->products->product as $field => $value ) {
  file_put_contents( '/vardump.txt' , $field . ' = ' . $value , FILE_APPEND );
}
produces:
field1 = 123
field2 = 
field3 = 

but I need field20 value...

 
So I get the error
Catchable fatal error: Object of class stdClass could not be converted to string in ...
Link to comment
https://forums.phpfreaks.com/topic/284776-unserialize/#findComment-1462391
Share on other sites

field3 contains an empty object, which you're trying to convert to a string. PHP cannot convert an object to a string unless you define a __toString method, but that is out of your control as the api is defining the serialized object.

 

If all need is field1 and field20 value then don't use a loop just do

$product = $result->products->product;

echo $product->field1;
echo $product->field20;

If you want to write these values to your var_dump.txt file then use

$product = $result->products->product;
$txt = "field1 = {$product->field1}
field20 = {$product->field20}";

file_put_contents( '/vardump.txt' , $txt, FILE_APPEND );
Link to comment
https://forums.phpfreaks.com/topic/284776-unserialize/#findComment-1462397
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.