Make selection in array

This is a discussion on Make selection in array within the PHP Language forums, part of the PHP Programming Forums category; Hi there. I have this array: name => john, age => 45, profession => teacher name => hank, age => ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-09-2006
frizzle
 
Posts: n/a
Default Make selection in array

Hi there.
I have this array:

name => john, age => 45, profession => teacher
name => hank, age => 22, profession => student
name => mary, age => 36, profession => dancer
etc.
etc.

It's going to be a big array.

Now what i wonder, how can i do the following;
while( $array['name'] == 22 )
{

do something.

}

I want to prevent the following:
while( $array )
{

if( 'name' == 22 ){

do something

}

}

Because this would create a big loop everytime.

Greetings Frizzle.

Reply With Quote
  #2 (permalink)  
Old 01-09-2006
Marcin Dobrucki
 
Posts: n/a
Default Re: Make selection in array


Even in the first case, you will need to loop through the array to
pick up the "rows" where age=22 (I assume the 'name' was a typo). The
question also is what is a big loop? 100 iterations? 10000000 iterations?

/m

frizzle wrote:
> Hi there.
> I have this array:
>
> name => john, age => 45, profession => teacher
> name => hank, age => 22, profession => student
> name => mary, age => 36, profession => dancer
> etc.
> etc.
>
> It's going to be a big array.
>
> Now what i wonder, how can i do the following;
> while( $array['name'] == 22 )
> {
>
> do something.
>
> }
>
> I want to prevent the following:
> while( $array )
> {
>
> if( 'name' == 22 ){
>
> do something
>
> }
>
> }
>
> Because this would create a big loop everytime.
>
> Greetings Frizzle.
>

Reply With Quote
  #3 (permalink)  
Old 01-09-2006
frizzle
 
Posts: n/a
Default Re: Make selection in array

Marcin Dobrucki wrote:
> Even in the first case, you will need to loop through the array to
> pick up the "rows" where age=22 (I assume the 'name' was a typo). The
> question also is what is a big loop? 100 iterations? 10000000 iterations?
>
> /m
>
> frizzle wrote:
> > Hi there.
> > I have this array:
> >
> > name => john, age => 45, profession => teacher
> > name => hank, age => 22, profession => student
> > name => mary, age => 36, profession => dancer
> > etc.
> > etc.
> >
> > It's going to be a big array.
> >
> > Now what i wonder, how can i do the following;
> > while( $array['name'] == 22 )
> > {
> >
> > do something.
> >
> > }
> >
> > I want to prevent the following:
> > while( $array )
> > {
> >
> > if( 'name' == 22 ){
> >
> > do something
> >
> > }
> >
> > }
> >
> > Because this would create a big loop everytime.
> >
> > Greetings Frizzle.
> >


'Name' was a sloppy typo indeed :$, the array is going to be ca. 150
iterations, but with big chunks of text in them....

Frizzle.

Reply With Quote
  #4 (permalink)  
Old 01-10-2006
Michael Austin
 
Posts: n/a
Default Re: Make selection in array

frizzle wrote:

> Hi there.
> I have this array:
>
> name => john, age => 45, profession => teacher
> name => hank, age => 22, profession => student
> name => mary, age => 36, profession => dancer
> etc.
> etc.
>
> It's going to be a big array.
>
> Now what i wonder, how can i do the following;
> while( $array['name'] == 22 )
> {
>
> do something.
>
> }
>
> I want to prevent the following:
> while( $array )
> {
>
> if( 'name' == 22 ){
>
> do something
>
> }
>
> }
>
> Because this would create a big loop everytime.
>
> Greetings Frizzle.
>


There are many ways to accomplish the end goal and there are many methods and
functions and constructs and tools to accomplish those goals.

I would use a database interface - like mysql. then select only the data you
want/need.

psuedo-code

$array = "select name,age,profession from mytable where name = 22"

foreach ($array as $x)
{ do something
}

or if you do not want to see information where the age = 22 then:

$array = "select name,age,profession from mytable where name <> 22"

foreach ($array as $x)
{ do something
}

you can even use case statements do to other things if
$ select name,case when age = 22 then age = 0 else age=age end,profession
from mytable where age < 25;

Now read and act on only the required data.

--
Michael Austin.
Everything is easier when the appropriate tool is used. Ever try removing a
tire with just a screwdriver??? (neither have I)
Reply With Quote
  #5 (permalink)  
Old 01-10-2006
Marcin Dobrucki
 
Posts: n/a
Default Re: Make selection in array

frizzle wrote:

> 'Name' was a sloppy typo indeed :$, the array is going to be ca. 150
> iterations, but with big chunks of text in them....


IMHO, 150 iterations isin't very much. Just make it work, and then
when it does, you can ponder if it can be optimized. If you are a DB
backend, then do as Michael suggested.

/M
Reply With Quote
  #6 (permalink)  
Old 01-10-2006
frizzle
 
Posts: n/a
Default Re: Make selection in array


Marcin Dobrucki wrote:
> frizzle wrote:
>
> > 'Name' was a sloppy typo indeed :$, the array is going to be ca. 150
> > iterations, but with big chunks of text in them....

>
> IMHO, 150 iterations isin't very much. Just make it work, and then
> when it does, you can ponder if it can be optimized. If you are a DB
> backend, then do as Michael suggested.
>
> /M


I am at a DB backend, but i want to prevent running unnessecary
queries, since they burden the DB when not needed. I guess i'll just
run through the array a couple of times then.
Thanks anyway. I was hoping for a sort of a DB query kind of solution,
like in my example,
regarding the items in the array as fiels names ...

while( $array['fieldname'] == 22 ) etc.

Too bad, but i got it working, and that's what's most important.

Greetings Frizzle.

Reply With Quote
  #7 (permalink)  
Old 01-11-2006
Colin Fine
 
Posts: n/a
Default Re: Make selection in array

frizzle wrote:
> Marcin Dobrucki wrote:
>
>>frizzle wrote:
>>
>>
>>>'Name' was a sloppy typo indeed :$, the array is going to be ca. 150
>>>iterations, but with big chunks of text in them....

>>
>> IMHO, 150 iterations isin't very much. Just make it work, and then
>>when it does, you can ponder if it can be optimized. If you are a DB
>>backend, then do as Michael suggested.
>>
>> /M

>
>
> I am at a DB backend, but i want to prevent running unnessecary
> queries, since they burden the DB when not needed. I guess i'll just
> run through the array a couple of times then.
> Thanks anyway. I was hoping for a sort of a DB query kind of solution,
> like in my example,
> regarding the items in the array as fiels names ...
>
> while( $array['fieldname'] == 22 ) etc.
>
> Too bad, but i got it working, and that's what's most important.
>
> Greetings Frizzle.
>


So, you're saying, How can I get DB functionality, oh and I don't want
to use my DB?!

If you're really concerned about performance, and you're going to do a
lot of searching by age, you can create an index by age, but you'll have
to cope with multiple elements:

$by_age = array();

foreach ($array as $item) {
$age = $item['age'];
if (!array_key_exists($age, $by_age)) {
$by_age[$age] = array();
}
$by_age[$age][] = $item;
};

Then you can go
foreach ($by_age[22] as $item) {
...
}

!!Not tested.!!


[Note that this will make the items in $by_age copies of those in
$array. If you need them to be references to the same object - because
you're going to alter them in one or other array and access them in the
other - you'll have to use references. IIRC this is tricky in PHP4,
because 'foreach' always gives you copies, but in PHP5 you can ask for
references.]

Colin
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:17 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0