473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Mar 25 '08 #1
3 2261
..oO(Dan Soendergaard)
>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().
>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
Mar 25 '08 #2
Dan Soendergaard wrote:
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.
js*******@attglobal.net
==================

Mar 25 '08 #3
>
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!
Mar 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Cro | last post by:
Hello Access Developers, I'd like to know if it is possible to perform a count in an expression that defines a control source. My report is based on a query. In my report, I want a text box to...
2
by: SJM | last post by:
I have a report that displays records of real estate properties. It is possible for each property to appear a number of times for various reasons. Each record however is unique. What I would like...
3
by: Zb Bornemann | last post by:
Hi All, Ok. Here's what my table looks like: Field1 Field2 Field3 Field4... Record1 1 3 2 9 Record2 9 1 9 ...
1
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 ...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
5
by: Dean | last post by:
Hi, I have a table with non-unique identifiers. I need to take all the values with the same ID's and combine them into one field with a semicolon as a seperator. These values may exceed 255...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.