Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9073 > unrolled thread

Help with searching arrays of objects

Started bydaveh@allheller.net
First post2013-04-17 17:47 -0700
Last post2013-04-19 10:18 +0200
Articles 7 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  Help with searching arrays of objects daveh@allheller.net - 2013-04-17 17:47 -0700
    Re: Help with searching arrays of objects Jerry Stuckle <jstucklex@attglobal.net> - 2013-04-18 09:24 -0400
      Re: Help with searching arrays of objects daveh@allheller.net - 2013-04-18 08:35 -0700
        Re: Help with searching arrays of objects Denis McMahon <denismfmcmahon@gmail.com> - 2013-04-18 18:10 +0000
          Re: Help with searching arrays of objects The Natural Philosopher <tnp@invalid.invalid> - 2013-04-18 19:29 +0100
        Re: Help with searching arrays of objects Jerry Stuckle <jstucklex@attglobal.net> - 2013-04-18 14:20 -0400
        Re: Help with searching arrays of objects "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-04-19 10:18 +0200

#9073 — Help with searching arrays of objects

Fromdaveh@allheller.net
Date2013-04-17 17:47 -0700
SubjectHelp with searching arrays of objects
Message-ID<11982693-fefb-4ee3-97ee-1f7b7abd7ca4@googlegroups.com>
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

[toc] | [next] | [standalone]


#9082

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-04-18 09:24 -0400
Message-ID<kkos1r$4m9$1@dont-email.me>
In reply to#9073
On 4/17/2013 8:47 PM, daveh@allheller.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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#9083

Fromdaveh@allheller.net
Date2013-04-18 08:35 -0700
Message-ID<63fdbbe0-a8e5-43ad-ba78-2155c41777a5@googlegroups.com>
In reply to#9082
On Thursday, April 18, 2013 9:24:50 AM UTC-4, Jerry Stuckle wrote:
> On 4/17/2013 8:47 PM, daveh@allheller.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@attglobal.net
> 
> ==================

Yeah I know its still a work in progress. the objects in question are properties of a database protocol over tcp/ip (do a google search for GT.M mumps) of which I am trying to upgrade a database driver written in 2001 using php version 4.0.6. The original code was intended to document how the protocol works and provide access to it using php. The code was very poorly written and made heavy use of the $GLOBALS superglobal to access  variables by doing stuff like this "$GLOBALS[$m_name]->my_method" blah blah blah and did not use proper OOP methodology mostly because it was not available at that time. Ironically the mumps database names its database objects globals! which made reading the code even more confusing .I have about 90% of the project completed and I started the project in 2009.

[toc] | [prev] | [next] | [standalone]


#9085

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-04-18 18:10 +0000
Message-ID<kkpcug$o35$2@dont-email.me>
In reply to#9083
On Thu, 18 Apr 2013 08:35:27 -0700, daveh wrote:

> On Thursday, April 18, 2013 9:24:50 AM UTC-4, Jerry Stuckle wrote:
>> On 4/17/2013 8:47 PM, daveh@allheller.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.

Simplifying your array

>> > Array
>> > (
>> >      [0] => gnp Object
>> >          (
>> >              [m_name] => db_1
>> >          )
>> >      [1] => gnp Object
>> >          (
>> >              [m_name] => db_2
>> >          )
>> > )

>> > So I need a method to find the objects key using m_name's value.

>> 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

>> 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.

> Yeah I know its still a work in progress.

Assuming you have access to the code that defines a gnp class, add a 
method such as:

public function get_name() {
return $this->m_name;
}

Then, building on Jerry's foreach, if you just want to get the index 
value ($key) or the class member ($item) for the first (only, if you're 
sure there's only ever one) match:

foreach ($mylist as $key => $item)
   if ($item->get_name() == $desired_value)
      break;

// $key will contain the index of the desired item here
// and $item will contain the actual item

however if you want to process every matching element of the array in 
some way:

foreach ($mylist as $key => $item)
   if ($item->get_name() == $desired_value) {
      // do something here with the $key or $item
   }

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#9087

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-04-18 19:29 +0100
Message-ID<kkpe2v$hoj$2@news.albasani.net>
In reply to#9085
On 18/04/13 19:10, Denis McMahon wrote:
> On Thu, 18 Apr 2013 08:35:27 -0700, daveh wrote:
>
>> On Thursday, April 18, 2013 9:24:50 AM UTC-4, Jerry Stuckle wrote:
>>> On 4/17/2013 8:47 PM, daveh@allheller.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.
> Simplifying your array
>
>>>> Array
>>>> (
>>>>       [0] => gnp Object
>>>>           (
>>>>               [m_name] => db_1
>>>>           )
>>>>       [1] => gnp Object
>>>>           (
>>>>               [m_name] => db_2
>>>>           )
>>>> )
>>>> So I need a method to find the objects key using m_name's value.
>>> 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
>>> 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.
>> Yeah I know its still a work in progress.
> Assuming you have access to the code that defines a gnp class, add a
> method such as:
>
> public function get_name() {
> return $this->m_name;
> }
>
> Then, building on Jerry's foreach, if you just want to get the index
> value ($key) or the class member ($item) for the first (only, if you're
> sure there's only ever one) match:
>
> foreach ($mylist as $key => $item)
>     if ($item->get_name() == $desired_value)
>        break;
>
> // $key will contain the index of the desired item here
> // and $item will contain the actual item
>
> however if you want to process every matching element of the array in
> some way:
>
> foreach ($mylist as $key => $item)
>     if ($item->get_name() == $desired_value) {
>        // do something here with the $key or $item
>     }
>
I suspect that key value pairs are hashed/indexed in some way in PHP as 
foreach loops seem to run very much faster than I expected even on large 
arrays.


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#9086

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-04-18 14:20 -0400
Message-ID<kkpdbh$bfq$1@dont-email.me>
In reply to#9083
On 4/18/2013 11:35 AM, daveh@allheller.net wrote:
> On Thursday, April 18, 2013 9:24:50 AM UTC-4, Jerry Stuckle wrote:
>> On 4/17/2013 8:47 PM, daveh@allheller.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@attglobal.net
>>
>> ==================
>
> Yeah I know its still a work in progress. the objects in question are properties of a database protocol over tcp/ip (do a google search for GT.M mumps) of which I am trying to upgrade a database driver written in 2001 using php version 4.0.6. The original code was intended to document how the protocol works and provide access to it using php. The code was very poorly written and made heavy use of the $GLOBALS superglobal to access  variables by doing stuff like this "$GLOBALS[$m_name]->my_method" blah blah blah and did not use proper OOP methodology mostly because it was not available at that time. Ironically the mumps database names its database objects globals! which made reading the code even more confusing .I have about 90% of the project completed and I started the project in 2009.
>

Ouch - yes, I understand how frustrating it can be.  Rewriting poor code 
is NOT one of my favorite pastimes! :)

But even though PHP 4.0.6 didn't have a lot of OO stuff available, that 
doesn't provide an excuse for such bad code.  Unfortunately, there are a 
lot of bad coders out there (and not just in PHP).

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

[toc] | [prev] | [next] | [standalone]


#9089

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-04-19 10:18 +0200
Message-ID<atccvhF3dnlU1@mid.uni-berlin.de>
In reply to#9083
Am 18.04.2013 17:35, schrieb daveh@allheller.net:
> ==================
> 
> Yeah I know its still a work in progress. the objects in question are properties of a database protocol over tcp/ip (do a google search for GT.M mumps) of which I am trying to upgrade a database driver written in 2001 using php version 4.0.6. The original code was intended to document how the protocol works and provide access to it using php. The code was very poorly written and made heavy use of the $GLOBALS superglobal to access  variables by doing stuff like this "$GLOBALS[$m_name]->my_method" blah blah blah and did not use proper OOP methodology mostly because it was not available at that time. Ironically the mumps database names its database objects globals! which made reading the code even more confusing .
> 

A good hint, you could have said this in the first post, because it explains a lot.

MUMPS is "persistent global variables" as I read it. Maybe you could use the
array_walk* functions to search the global data, and use the newest closure with
"use" keyword to parametrize your search.

/Str.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web