Jump to content

Warning: array_values() expects parameter 1 to be array, object given


Fran
 Share

Recommended Posts

I'm getting this warning when I try to print an array:

Warning: array_values() expects parameter 1 to be array, object given...

This is my code:

$array = $page->get("planos");
print_r(array_values($array));

I've always thought that $page->get will give you an array, but I'm not so sure now.

Thanks for any help.

Link to comment
Share on other sites

What $page->get returns depends on the field type, but you'll hardly ever get back a plain array. Btw., $page->get("planos") is the same as $page->planos. For image and file fields, the return value (unless null/empty or set to "Single item") is a PageImages object, which, while it inherits from WireArray and thus implements an iterator so you can run a foreach loop over it, isn't the same as a plain array.

If you do need a plain array for some reason (in most cases you won't), you can call getArray() or getValues() on classes inheriting from WireArray.

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...