FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Help with searching arrays of objects
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Help with searching arrays of objects [message #181138 is a reply to message #181129] Thu, 18 April 2013 13:24 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 4/17/2013 8:47 PM, daveh(at)allheller(dot)net wrote:
>
> Hello,
>
> I know this has probably been asked before but I'm still a bit of a noob.
> Given the following structure I need a way to get a "key" to the desired object using a members value.
>
> Array
> (
> [0] => gnp Object
> (
> [m_server:gnp:private] => 127.0.0.1
> [m_port:gnp:private] => 30000
> [m_dat_file:gnp:private] => /tmp/data.file
> [m_socket:gnp:private] => tcp_socket Object
> (
> [m_address] => 127.0.0.1
> [m_portno] => 30000
> [m_tcpSocket] => Resource id #13
> )
>
> [m_proc_num:gnp:private] => 132
> [m_open_region:gnp:private] => region Object
> (
> [m_region_no] => 0
> [m_nullsubs] => 0
> [m_max_rec_len] => 256
> [m_max_subsc_len] => 64
> )
>
> [m_cur_region_no:gnp:private] => 0
> [m_name] => db_1
> )
>
> [1] => gnp Object <--------- This is the "object" I want and I need the "key" which in this case is 1
> (
> [m_server:gnp:private] => 127.0.0.1
> [m_port:gnp:private] => 30000
> [m_dat_file:gnp:private] => /tmp/data.file
> [m_socket:gnp:private] => tcp_socket Object
> (
> [m_address] => 127.0.0.1
> [m_portno] => 30000
> [m_tcpSocket] => Resource id #14
> )
>
> [m_proc_num:gnp:private] => 133
> [m_open_region:gnp:private] => region Object
> (
> [m_region_no] => 0
> [m_nullsubs] => 0
> [m_max_rec_len] => 256
> [m_max_subsc_len] => 64
> )
>
> [m_cur_region_no:gnp:private] => 0
> [m_name] => db_2 <----------------- Using the "key" m_name's value db_2 or db_1 if I want the other object
> )
>
> )
>
> So I need a method to find the objects key using m_name's value. I'm thinking a foreach but not sure how to implement it in this case. The structure shown has been snipped quite a bit for brevity. There could be any number of these gnp
> objects I need the key as a way to access them and perform class methods on them. Obviously the m_name value desired would be assigned to a variable first.
> And there could be any number of objects stored in this array of objects.
>
> Dave
>

About the only thing you can do is use a foreach() loop to step through
the array items, comparing m_name in each object to the desired value, i.e.

for ($mylist as $key => $item) {
if ($item->m_name = $desired_value) {
// $key will contain the index of the desired item here

But some other questions - where did the objects come from? If they
were from something like a SQL database, you should use SQL to find the
desired item.

Additionally, if these are class objects, you should study up on object
oriented techniques; your data should be private and you should have
access methods to get and set the values. This ensures validity of the
objects at all times.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: display two values on page
Next Topic: Problema con variables de sesion...
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 11:53:21 GMT 2024

Total time taken to generate the page: 0.03811 seconds