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 #181142 is a reply to message #181139] Thu, 18 April 2013 18:20 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
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
==================
[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: Sun Nov 24 17:53:28 GMT 2024

Total time taken to generate the page: 0.04013 seconds