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
Switch to threaded view of this topic Create a new topic Submit Reply
Help with searching arrays of objects [message #181129] Thu, 18 April 2013 00:47 Go to next message
daveh is currently offline  daveh
Messages: 18
Registered: March 2013
Karma: 0
Junior Member
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
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 next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Re: Help with searching arrays of objects [message #181139 is a reply to message #181138] Thu, 18 April 2013 15:35 Go to previous messageGo to next message
daveh is currently offline  daveh
Messages: 18
Registered: March 2013
Karma: 0
Junior Member
On Thursday, April 18, 2013 9:24:50 AM UTC-4, Jerry Stuckle wrote:
> 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
>
> ==================

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.
Re: Help with searching arrays of objects [message #181141 is a reply to message #181139] Thu, 18 April 2013 18:10 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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(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.

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(at)gmail(dot)com
Re: Help with searching arrays of objects [message #181142 is a reply to message #181139] Thu, 18 April 2013 18:20 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/18/2013 11:35 AM, daveh(at)allheller(dot)net wrote:
> On Thursday, April 18, 2013 9:24:50 AM UTC-4, Jerry Stuckle wrote:
>> 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
>>
>> ==================
>
> 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(at)attglobal(dot)net
==================
Re: Help with searching arrays of objects [message #181143 is a reply to message #181141] Thu, 18 April 2013 18:29 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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(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.
> 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.
Re: Help with searching arrays of objects [message #181145 is a reply to message #181139] Fri, 19 April 2013 08:18 Go to previous message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 18.04.2013 17:35, schrieb daveh(at)allheller(dot)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.
  Switch to threaded view of this topic Create a new topic Submit Reply
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 Dec 18 02:04:24 GMT 2024

Total time taken to generate the page: 0.02144 seconds