Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 25th, 2008, 07:45 PM
Dan Soendergaard
Guest
 
Posts: n/a
Default Count the number of unique values of field in object in array

Hi all,

I'm sure there is a simple solution to this problem, although it
sounds rather complex. I have an array of Entry objects with the
fields: ip, date (unix time), name. So, the array might contain this
data,

000.000.000.000 987678 hello
001.001.001.001 558821 hello
000.000.000.000 287999 something

What I want to do is to sort the array of objects by a field.
Essentially, I'd like something like:

sort($data, $object->name);
or
sort($data, $object->date);

Is there a way to do this in PHP, or do I have to write a function
myself? If yes, do you have any suggestions on how to do this?

Also, is there a way to make array_count_values() and array_unique()
operate on fields in objects?

Thank you very much in advance!

- Dan
  #2  
Old March 25th, 2008, 09:45 PM
Michael Fesser
Guest
 
Posts: n/a
Default Re: Count the number of unique values of field in object in array

..oO(Dan Soendergaard)
Quote:
>I'm sure there is a simple solution to this problem, although it
>sounds rather complex. I have an array of Entry objects with the
>fields: ip, date (unix time), name. So, the array might contain this
>data,
>
>000.000.000.000 987678 hello
>001.001.001.001 558821 hello
>000.000.000.000 287999 something
>
>What I want to do is to sort the array of objects by a field.
>Essentially, I'd like something like:
>
>sort($data, $object->name);
>or
>sort($data, $object->date);
>
>Is there a way to do this in PHP, or do I have to write a function
>myself? If yes, do you have any suggestions on how to do this?
You would have to write your own function and use it with usort().
Quote:
>Also, is there a way to make array_count_values() and array_unique()
>operate on fields in objects?
Not really, but you could wrap the entire list into an object and add a
method to return a particular column from the list, e.g.

$data->getColumn(0);

would return an array containing all IP addresses, the parameter 1 would
return an array with all timestamps, 2 all the names. This would allow
you to use a lot of array functions on just a particular column. Even
the sorting could then probably be done with array_multisort().

HTH
Micha
  #3  
Old March 25th, 2008, 09:45 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: Count the number of unique values of field in object in array

Dan Soendergaard wrote:
Quote:
Hi all,
>
I'm sure there is a simple solution to this problem, although it
sounds rather complex. I have an array of Entry objects with the
fields: ip, date (unix time), name. So, the array might contain this
data,
>
000.000.000.000 987678 hello
001.001.001.001 558821 hello
000.000.000.000 287999 something
>
What I want to do is to sort the array of objects by a field.
Essentially, I'd like something like:
>
sort($data, $object->name);
or
sort($data, $object->date);
>
Is there a way to do this in PHP, or do I have to write a function
myself? If yes, do you have any suggestions on how to do this?
>
Also, is there a way to make array_count_values() and array_unique()
operate on fields in objects?
>
Thank you very much in advance!
>
- Dan
>
Check out usort().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  #4  
Old March 26th, 2008, 06:55 AM
Dan Soendergaard
Guest
 
Posts: n/a
Default Re: Count the number of unique values of field in object in array

>
Quote:
You would have to write your own function and use it with usort().
>
Ok, I'll look into writing such a function. Thank you for your help!
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles