1

Im looking to use a PHP array obtained from a RESTful service.

They array is just a simple array outputted as follows,

array (
  0 => 
  stdClass::__set_state(array(
     'id' => '375',
     'primary_name' => 'Beaufort 3',
     'price' => '',
     'sqft' => '2435',
     'bdrm' => '3',
     'bthm' => '2.5',
     'display_title' => 'Traditional A1',
     'full_img_path' => '',
     'thumb_path' => '',
     'available_in' => 'a:2:{i:0;s:1:"2";i:1;s:1:"5";}',
     'display_first' => '',
  )),
)

I'm obtaining the data using file_get_contents but of course its no longer an array at this point. What is the best way to convert this back to a usable array?

2
  • Can you modify the content of the file? If you could save the file content as a serialized data you would be able to get the content of the file and unserialize it to obtain the array Commented Aug 7, 2014 at 20:03
  • @Javad Yes i can modify it to be serialized if necessary, however by default the service just offers PHP, XML, JSON and HTML output. I guess im just curious as to why it would offer a PHP array output and more so how i can specifically use said PHP output in its default state. Commented Aug 7, 2014 at 20:08

2 Answers 2

5

I have never seen a service that provides this, probably because it is something people would avoid using, but it does look like it could be something they intend for you to be able to use with eval(). I suppose this could be intended as a convenience. I think they have made an error, though. If you use var_export, (which I assume is how this was generated) on an array like this:

$example = array (
   array(
     'id' => '375',
     'primary_name' => 'Beaufort 3',
     'price' => '',
     'sqft' => '2435',
     'bdrm' => '3',
     'bthm' => '2.5',
     'display_title' => 'Traditional A1',
     'full_img_path' => '',
     'thumb_path' => '',
     'available_in' => 'a:2:{i:0;s:1:"2";i:1;s:1:"5";}',
     'display_first' => '',
   )
);

then you would get something you could eval into a variable and use in your code. However, if you var_export an array of anonymous objects instead of an array of associative arrays, you will get the type of response you are getting, which I don't know of any way to use. I would guess they are var_exporting the results of a query and they are using FETCH_OBJ instead of FETCH_ASSOC for the fetch style.

EDIT: I was looking through the comments on var_export after writing this and came across this way of doing it that should work.

$str = str_replace("stdClass::__set_state", "(object)", $str);
eval('$array=' . $str . ';');

But just because something is possible doesn't mean we should do it.

Sign up to request clarification or add additional context in comments.

Comments

3

You can convert it back into an array with eval(). http://php.net/manual/en/function.eval.php

eval('$array='.$response.';');

However, this can be dangerous, because eval will take any PHP code given it - if the service is compromised, your code would execute anything passed to it. JSON, if the service supports it, is much safer and natively supported in PHP since 5.2 via json_decode(). http://php.net/manual/en/function.json-decode.php

1 Comment

Thanks for the response. Eval doesnt seem to work in this case, as it hangs on the stdClass { Fatal error: Call to undefined method stdClass::__set_state() }

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.