PHP,stdClass的,选择元素包含specifed元素 [英] PHP, stdClass, select elements contains specifed element

查看:177
本文介绍了PHP,stdClass的,选择元素包含specifed元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个stClass对象是这样的:

I have an stClass object like this:

object(stdClass)#2 (6) {
  [0]=>
  object(stdClass)#44 (2) {
    ["uid"]=>
    int(3232)
    ["type"]=>
    string(7) "sibling"
  }
  [1]=>
  object(stdClass)#43 (2) {
    ["uid"]=>
    int(32323)
    ["type"]=>
    string(7) "sibling"
  }
  [2]=>
  object(stdClass)#42 (2) {
    ["uid"]=>
    int(3213)
    ["type"]=>
    string(10) "grandchild"
  }
  [3]=>
  object(stdClass)#41 (3) {
    ["uid"]=>
    int(-680411188)
    ["type"]=>
    string(6) "parent"
  }
  [4]=>
  object(stdClass)#40 (3) {
    ["uid"]=>
    int(-580189276)
    ["type"]=>
    string(6) "parent"
  }
  [5]=>
  object(stdClass)#39 (2) {
    ["uid"]=>
    int(3213)
    ["type"]=>
    string(7) "sibling"
  }
}

我怎样才能得到与类型元素的特定值的元素?
例如,如果我选择家长,我想得到这样的:

How can I get elements with specified value of type element? For example, if I select "parent", I wanna get this:

object(stdClass)#2 (6) {
  [3]=>
  object(stdClass)#41 (3) {
    ["uid"]=>
    int(-680411188)
    ["type"]=>
    string(6) "parent"
  }
  [4]=>
  object(stdClass)#40 (3) {
    ["uid"]=>
    int(-580189276)
    ["type"]=>
    string(6) "parent"
  }
}

我知道了,如何将它与写的foreach 如果,但我希望有另一种方式。谢谢

I know, how to write it with "foreach" and "if", but I hope that there is another way. Thanks

推荐答案

您外部对象其实,变相的数组。您可以通过类型转换将其转换为真正的数组:

Your outer object is in fact, an array in disguise. You can convert it to a real array by typecasting:

$arr = (array)$obj;

然后你可以使用:

Then you can use:

$filtered = array_filter(
    $arr,
    function($item) {
        return $item->type == 'parent';
    }
);

要得到一个仅包含你需要的。对象的数组

to get an array that contains only the objects you need.

这篇关于PHP,stdClass的,选择元素包含specifed元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆