1

I did a var dump of a php object $grid, and there is this property that I need to access:

["wpupg_post_types"]=> array(1) { [0]=> string(21) "a:1:{i:0;s:4:"post";}" } 

I need to get the word "post" out of that. I tried doing

$posttype = $grid->wpupg_post_types; if (in_array("post", $posttype)) { echo "post"; } 

But that didn't work. And if I try var_dump($grid->wpupg_post_types); it returns NULL.

Do you know how I can do it?

3
  • can you print all $grid variable? Commented May 31, 2017 at 14:28
  • Sure! Just updated my question. Commented May 31, 2017 at 14:30
  • UHm too long and bad to read, try to remove many parts to view only the single problem of your object Commented May 31, 2017 at 14:31

2 Answers 2

2

The variable is an array of strings that are serialized:

a:1:{i:0;s:4:"post";} 

Pull the first item off and then pass it to unserialize() to turn it into an array:

$result = unserialize(array_shift($grid->wpupg_post_types)); 

This yields:

Array ( [0] => post ) 

Note: This assumes the property is public.

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

5 Comments

Thank you. When I try this, I get no return. When I try var_dump($result), it gives me bool(false). Do you know why that is?
Is wpupg_post_types public?
That must be the problem. I don't think it is.
The class probably has some getter method like get_post_types() or something that you can call to get the value.
Ah ha! I checked out the class, and found $grid->post_types. I don't understand why since post_types isn't a property? But nevertheless, this worked: if( in_array( 'post', $grid->post_types() ) )
2

$posttype = $grid->wpupg_post_types; contains an array of one element with a serialized array with post.

php > $array = [serialize(['post'])]; php > var_dump($array); php shell code:1: array(1) { [0] => string(21) "a:1:{i:0;s:4:"post";}" } 

To check if the post is inside the array you need to do another kind of check

php > var_dump(in_array('post', unserialize($array[0]))); php shell code:1: bool(true) 

Your particular case should be

if(in_array('post', unserialize($grid->wpupg_post_types[0]))) { echo 'post'; } 

EDIT: here my interactive shell

$ php -a Interactive shell php > $array = [serialize(['post'])]; php > var_dump($array); php shell code:1: array(1) { [0] => string(21) "a:1:{i:0;s:4:"post";}" } php > var_dump(in_array('post', unserialize($array[0]))); php shell code:1: bool(true) php > 

2 Comments

Thank you. I can't seem to get it to work. If I try $result = unserialize($grid->wpupg_post_types[0]) and then do a var_dump on $result... it says bool(false).
An upvote for your time spent on this. Thank you! I found a solution (see my comment to Alex Howansky).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.