Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #9085
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Help with searching arrays of objects |
| Date | 2013-04-18 18:10 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <kkpcug$o35$2@dont-email.me> (permalink) |
| References | <11982693-fefb-4ee3-97ee-1f7b7abd7ca4@googlegroups.com> <kkos1r$4m9$1@dont-email.me> <63fdbbe0-a8e5-43ad-ba78-2155c41777a5@googlegroups.com> |
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
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web