shaunie Posted December 15, 2013 Share Posted December 15, 2013 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!!! } Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 15, 2013 Share Posted December 15, 2013 What api are you using as the serialized array structure you posted does not seem to be in php format. What does this return printf('<pre>%s</pre>', print_r($result, true)); Quote Link to comment Share on other sites More sharing options...
shaunie Posted December 15, 2013 Author Share Posted December 15, 2013 My apologies, I used this to get the contents of $result var: file_put_contents( '/vardump.txt' , var_export($result, true) , FILE_APPEND ); Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 15, 2013 Share Posted December 15, 2013 (edited) Never used var_export. Used to the output from print_r 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 December 15, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
shaunie Posted December 15, 2013 Author Share Posted December 15, 2013 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 ... Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 15, 2013 Share Posted December 15, 2013 (edited) 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 December 15, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
shaunie Posted December 15, 2013 Author Share Posted December 15, 2013 Thank you, I need to loop through though as there may be more than one product... Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 16, 2013 Share Posted December 16, 2013 (edited) Try this: $result = unserialize($api->getResponseSerialized()); foreach ($result->products as $product) { edit $product->field1; } Edited December 16, 2013 by scootstah Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.