0

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
        )

)
10
  • What do you mean by "similar values"? Which values? How "close" do the values have to be to each other to be "similar"? Commented Feb 9, 2012 at 23:51
  • The example is array items 2, 3 and 4 because they all have same category_parent => 4 Commented Feb 9, 2012 at 23:55
  • Okay, so loop through the array and only pick out those subarrays with category_parent==4. Commented Feb 9, 2012 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. Commented Feb 10, 2012 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> Commented Feb 10, 2012 at 0:08

3 Answers 3

1

Use array_filter

$filtered = array_filter($original, function ($class) {
    return $class->category_parent === 4;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Stored in a new array.

$result = array();

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

print_r($result);

3 Comments

He needs to check the multidimensional array for common values, not if value == x
@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.
I'm answering his specific need to filter the category_parent :)
0
foreach ($array as $key => $element){
  if ($element->category_parent == 4)
    $ret[] = $key;
}
//$ret is the final array

1 Comment

sorry that saves the indexes not the items. if you want to save the items, Muhammad Abrar's answer will do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.