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
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;
Edited by Ch0cu3r
Link to comment
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
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 );
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.