[英]Get property from php object
I did a var dump of a php object $grid
, and there is this property that I need to access: 我做了php对象
$grid
的var转储,并且需要访问此属性:
["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);
如果我尝试
var_dump($grid->wpupg_post_types);
it returns NULL. 它返回NULL。
Do you know how I can do it? 你知道我该怎么做吗?
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: 拉出第一项,然后将其传递给
unserialize()
以将其变成数组:
$result = unserialize(array_shift($grid->wpupg_post_types));
This yields: 这样产生:
Array
(
[0] => post
)
Note: This assumes the property is public. 注意:这假定该属性是公共的。
$posttype = $grid->wpupg_post_types;
contains an array of one element with a serialized array with post. 包含一个元素的数组和带有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 >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.