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 8816847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:42:48+00:00 2026-06-14T04:42:48+00:00

am trying to print out only the unique values. since am receiving a huge

  • 0

am trying to print out only the unique values. since am receiving a huge object array from the I am trying to use now the ArrayObject class of PHP to iterate

$arrayobject = new ArrayObject($data);

$iterator = $arrayobject->getIterator();

while($iterator->valid()){
    echo $iterator->current()->USERID. " : " .$iterator->current()->SUBCATID."<br/>";
    $iterator->next();
}

here’s the current result of that

201087 : 1
201146 : 1
201087 : 3
201087 : 2

as you can see, the first data has two other duplicates
and also, the first and second data has similar subcatid..
the objective is, print only the unique userid and subcatid..
how to skip those duplicate data, given that sample code of mine
as a starting point ?

  • 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-14T04:42:49+00:00Added an answer on June 14, 2026 at 4:42 am

    Not quite sure I understand the question but maybe….
    You can either sort the array and remember the current userid so your script can skip duplicates until it reaches another id.

    <?php
    $data = data();
    usort(
        $data,
        function($a,$b) {
            return strnatcmp($a->USERID, $b->USERID);
        }
    );
    $current = null;
    foreach( $data as $e ) {
        if ( $current!=$e->USERID ) {
            $current = $e->USERID;
            echo $e->USERID, ' ', $e->SUBCATID, "\n";
        }
    }   
    
    
    function data() {
    
        $x = array(
            array(201087,1),
            array(201146,1),
            array(201087,3),
            array(201087,2),
            array(222222,3)
        );
        foreach($x as $y) {
            $o = new StdClass;
            $o->USERID = $y[0];
            $o->SUBCATID = $y[1];
            $data[] = $o;
        }
        return $data;
    }
    

    or the script remembers all previously processed ids, e.g. in a hashmap/array

    <?php
    $data = data();
    
    $processed = array();
    foreach( $data as $e ) {
        if ( !isset($processed[$e->USERID]) ) {
            $processed[$e->USERID] = true;
            echo $e->USERID, ' ', $e->SUBCATID, "\n";
        }
    }   
    
    
    function data() {
    
        $x = array(
            array(201087,1),
            array(201146,1),
            array(201087,3),
            array(201087,2),
            array(222222,3)
        );
        foreach($x as $y) {
            $o = new StdClass;
            $o->USERID = $y[0];
            $o->SUBCATID = $y[1];
            $data[] = $o;
        }
        return $data;
    }
    

    both scripts print

    201087 1
    201146 1
    222222 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to print the array but the out put contain only the
I'm trying to print out a html-table from db-table 'dimensions'. I only want dimensions
I am trying to print out documents in various different orders. Right now I
I'm trying to print a 2D array of random numbers from 1 to 15
I'm trying to figure out how to print only sub-category of a given category.
I've been trying to print out Google Calendars but they only print for letter
I'm trying to go through a file, and only print out words without a
I am trying to use a report to print out a list of users
I'm trying to print out only the number of rows containing user2_ids within my
Trying to print out the bits of each char stored in an array. I've

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.