Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
        )

)
share|improve this question
    
What do you mean by "similar values"? Which values? How "close" do the values have to be to each other to be "similar"? –  Jack Maney Feb 9 '12 at 23:51
    
The example is array items 2, 3 and 4 because they all have same category_parent => 4 –  leonel Feb 9 '12 at 23:55
    
Okay, so loop through the array and only pick out those subarrays with category_parent==4. –  Jack Maney Feb 9 '12 at 23:57
    
If you want something different, you're going to have to coherently explain what you want to do rather than give one example and hope that we're mind readers (Hint: we're not) and can divine what you actually want to do. –  Jack Maney Feb 10 '12 at 0:07
    
The problem is, I have to create HTML lists on the fly. so <ul><li>2</li><li>3</li><li>4</li></ul> –  leonel Feb 10 '12 at 0:08

3 Answers 3

Use array_filter

$filtered = array_filter($original, function ($class) {
    return $class->category_parent === 4;
});
share|improve this answer

Stored in a new array.

$result = array();

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

print_r($result);
share|improve this answer
    
He needs to check the multidimensional array for common values, not if value == x –  w0rldart Feb 9 '12 at 23:54
    
@w0rldart - OP is either unable or unwilling to elaborate on what he means other than he wants all of the subarrays with the category_parent value to equal 4. –  Jack Maney Feb 9 '12 at 23:58
    
I'm answering his specific need to filter the category_parent :) –  Muhammad Abrar Feb 10 '12 at 0:00
foreach ($array as $key => $element){
  if ($element->category_parent == 4)
    $ret[] = $key;
}
//$ret is the final array
share|improve this answer
    
sorry that saves the indexes not the items. if you want to save the items, Muhammad Abrar's answer will do –  chechopeefe Feb 9 '12 at 23:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.