|
Posted by Colin Fine on 01/11/06 01:08
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
[Back to original message]
|