473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.

Jan 9 '06 #1
6 1582

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.

Jan 9 '06 #2
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.

Jan 9 '06 #3
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)
Jan 9 '06 #4
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
Jan 10 '06 #5

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.

Jan 10 '06 #6
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
Jan 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: booner | last post by:
I have a form that when it loads I would like to highlight the values (from a DB) that have been selected in a multiple selection list (<select multiple="true">. function onLoad() {...
2
by: John Ryan | last post by:
I've a small bit of javascript on my site that has a from with 2 selection boxes, when you choose an option in the first box, the second one re-populates its list accordingly. But the second...
7
by: simondex | last post by:
Hi, Everyone! How could I make sure that nine values (from 0 to N) are all different without writing too many loops? I am not afraid of statistical parameters (e.g. coefficient of variation,...
13
by: ANSHUL | last post by:
PLEASE PROVIDE ME D SOLUTION CODE FOR DIS PROBLEM. SELECTION SORT IS BASED ON D FOLLOWING IDEA: SELECTING D LARGEST ARRAY ELEMENT AND SWAPPING IT WITH THE LAST ARRAY ELEMENT LEAVES AN UNSORTED...
3
by: peter.mosley | last post by:
I've tried googling for the answer to this problem, without any luck. I'm sure the truth must be out there somewhere! I have a multiselect listbox populated with many items (set by the RowSource...
1
by: neyugncul | last post by:
Okay, I have this homework assignment which is to sort a 2D array of characters using selection sort. I've written the selection sort but I can't seem to get it working. (it's not sorting!) ...
4
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
3
by: Hodld | last post by:
Hi, I'm creating a selection form with array data, using a for loop. This is my first time doing this, so it could be a pretty dumb mistake, but all the values end up printing on the line above...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.