PHP.遍历数组并获取具有共同属性的项目 [英] PHP. Loop through an array and get items with attributes in common

查看:38
本文介绍了PHP.遍历数组并获取具有共同属性的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历此数组并获取仅具有相似值的数组.在下面的示例中,我想要一个包含项2、3和4的数组,因为它们都具有相同的category_parent => 4.

How can I loop through this array and get arrays with only similar values. In the example below, I would like to have an array that contains items 2, 3 and 4 because they all have category_parent => 4 in common.

我需要遍历该数组,以便获得以下结果.

I need to loop through that array so that I get the following result.

<ul>
  <li>0</li>
  <li>1
    <ul>
      <li>2</li>
      <li>3</li>
      <li>4</li>
    </ul>
  </li>
  <li>5</li>
</ul>


Array
(
    [0] => stdClass Object
        (
            [term_id] => 3
            [name] => Comercial
            [slug] => comercial
            [term_group] => 0
            [term_taxonomy_id] => 3
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 1
            [cat_ID] => 3
            [category_count] => 1
            [category_description] => 
            [cat_name] => Comercial
            [category_nicename] => comercial
            [category_parent] => 0
        )

    [1] => stdClass Object
        (
            [term_id] => 4
            [name] => Escolar
            [slug] => escolar
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 0
            [cat_ID] => 4
            [category_count] => 0
            [category_description] => 
            [cat_name] => Escolar
            [category_nicename] => escolar
            [category_parent] => 0
        )

    [2] => stdClass Object
        (
            [term_id] => 5
            [name] => Kinder
            [slug] => kinder
            [term_group] => 0
            [term_taxonomy_id] => 5
            [taxonomy] => category
            [description] => 
            [parent] => 4
            [count] => 1
            [cat_ID] => 5
            [category_count] => 1
            [category_description] => 
            [cat_name] => Kinder
            [category_nicename] => kinder
            [category_parent] => 4
        )

    [3] => stdClass Object
        (
            [term_id] => 6
            [name] => Primaria
            [slug] => primaria
            [term_group] => 0
            [term_taxonomy_id] => 6
            [taxonomy] => category
            [description] => 
            [parent] => 4
            [count] => 1
            [cat_ID] => 6
            [category_count] => 1
            [category_description] => 
            [cat_name] => Primaria
            [category_nicename] => primaria
            [category_parent] => 4
        )

    [4] => stdClass Object
        (
            [term_id] => 7
            [name] => Secundaria
            [slug] => secundaria
            [term_group] => 0
            [term_taxonomy_id] => 7
            [taxonomy] => category
            [description] => 
            [parent] => 4
            [count] => 2
            [cat_ID] => 7
            [category_count] => 2
            [category_description] => 
            [cat_name] => Secundaria
            [category_nicename] => secundaria
            [category_parent] => 4
        )

    [5] => stdClass Object
        (
            [term_id] => 1
            [name] => Uncategorized
            [slug] => uncategorized
            [term_group] => 0
            [term_taxonomy_id] => 1
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 1
            [cat_ID] => 1
            [category_count] => 1
            [category_description] => 
            [cat_name] => Uncategorized
            [category_nicename] => uncategorized
            [category_parent] => 0
        )

)

推荐答案

使用 array_filter

$filtered = array_filter($original, function ($class) {
    return $class->category_parent === 4;
});

这篇关于PHP.遍历数组并获取具有共同属性的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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