Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:38:09+00:00 2026-06-17T12:38:09+00:00

I have an array like below Array ( [0] => stdClass Object ( [id]

  • 0

I have an array like below

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [cat_id] => 1
            [item_name]=>test1
            [cat_name] => Normal
        )

    [1] => stdClass Object
        (
            [id] => 2
            [cat_id] => 2
            [item_name]=>test2
            [cat_name] => Featured
        )

    [2] => stdClass Object
        (
            [id] => 3
            [cat_id] => 2
            [item_name]=>test3
            [cat_name] => Featured
        )  
)  

And I want the result to look like this

Normal

test1

Featured

test2  |  test3

I have try this so far:

<?php
foreach($rows as $row){
    echo '<h2>'.$row->cat_name.'</h2>';
    echo '<p>'.$row->item_name.'</p>';
}
?>

But it shows the heading with each of the item.Can someone help me to sort this out.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:38:10+00:00Added an answer on June 17, 2026 at 12:38 pm

    So you want to group them? Here’s a function for that:

    function groupBy($arr, $func) {
        $groups = [];
    
        foreach($arr as $item) {
            $group = $func($item);
    
            if(array_key_exists($group, $groups))
                $groups[$group][] = $item;
            else
                $groups[$group] = [$item];
        }
    
        return $groups;
    }
    

    Use it like so:

    $groups = groupBy($rows, function($row) { return $row->cat_name; });
    
    foreach($groups as $name => $items) {
        echo "<h2>$name</h2>";
    
        foreach($items as $item)
            echo "<p>{$item->item_name}</p>";
    }
    

    And here’s a demo. If you don’t have the luxury of PHP 5.3, then you can make it more specialized:

    function groupBy($arr, $prop) {
        $groups = array();
    
        foreach($arr as $item) {
            $group = $item->$prop;
    
            if(array_key_exists($group, $groups))
                $groups[$group][] = $item;
            else
                $groups[$group] = array($item);
        }
    
        return $groups;
    }
    ...
    $groups = groupBy($rows, 'cat_name');
    
    foreach($groups as $name => $items) {
        echo "<h2>$name</h2>";
    
        foreach($items as $item)
            echo "<p>{$item->item_name}</p>";
    }
    

    Demo on PHP 5.2.17

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array like this (below is the var_dump) array 0 => object(stdClass)[11]
I have an array of array like below with all numeric values. I want
I have an array like below, and I want to do the total of
If I have a stdClass object like the one below, which has three arrays
I have array like below the page. I want to find array number which
I have array converted into stdClass object like this, stdClass Object ( [1339697186] =>
I have an array like the one below given. I want to echo this
I have an array which appears like the one below. I want to slice
I have an Array like below in PHP array( (int) 0 => array( 'id'
i have an array like below int[] array = new array[n];// n may be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.