PHP。循环遍历数组并获取具有共同属性的项目

时间:2012-02-09 23:45:49

标签: php arrays

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

我需要遍历该数组,以便得到以下结果。

<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
        )

)

3 个答案:

答案 0 :(得分:1)

使用array_filter

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

答案 1 :(得分:0)

存储在新数组中。

$result = array();

foreach ($array as $element) {
    if($element->category_parent == 4) {
        $result[] = $element;
    }
}

print_r($result);

答案 2 :(得分:0)

foreach ($array as $key => $element){
  if ($element->category_parent == 4)
    $ret[] = $key;
}
//$ret is the final array