1

Using PHP, I printed out an array ($result) using print_r. But the format in which the array has been returned is a new one on me. Specifically, how do I retrieve the text found in the field "reviews_review" ("Wow, what a great...") when it's found in this particular format with its multiple values? Example:

stdClass Object
 (
 [node_reviews_nid] => 5270
 [reviews_review] => a:2:{s:5:"value";s:38:"Wow, what a great place.  The bestest!";s:6:"format";s:13:"filtered_html";}

Those extra values and curly brackets have got me stumped. If I wanted the node_reviews_nid I could simply use $result->node_reviews_nid" - but doing that to get reviews_review just retrieves too much ie,

a:2:{s:5:"value";s:38:"Wow, what a great place.  The bestest!";s:6:"format";s:13:"filtered_html";})

2 Answers 2

2

The odd curly brackets you're seeing are the result of PHP's serialize function.

Basically it's intended to convert complex structures like objects, nested arrays and so on into simple strings which are easier and safer to transfer, for example over HTTP. Think of it as JSON representation, but specific to PHP (do not attempt to json_decode() a serialized value).

Also worth noting is that serialize-d string has a maximum length, beyond which it's simply truncated, so avoid using it for very large structures - you'll lose data.

The reverse of that is unserialize. So to read the review's text, you would first have to unserialize the "reviews_review" property, then reference the "value" index of the resulting array.

$review_data = unserialize($result->reviews_review);
$review_text = $review_data['value'];
print($review_text);
Sign up to request clarification or add additional context in comments.

1 Comment

A-ha! Thanks much for that. Perfect. I had a devil of a time figuring out how to tackle that - because it was more than a little tough to figure out what search terms to use to hone in! Thanks to both you and RiggsFolly for turning in great answers (separated by just a minute - that's service!) Thanks again - K
1

The data in reviews_review looks like a serialized array i.e. written using the serialize() function

So you will need to unserialize it using unserialize() before you can use it

$review = unserialize($obj->reviews_review);
print_r($review);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.