The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 08:28:31 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Iterating Through An Associative Array  (Read 1209 times)
cdc
Expert
****
Offline Offline

Posts: 105


View Profile
« on: May 28, 2007, 08:47:38 AM »

I don't use associative arrays as much as I should, but I'm starting to use them more often now.

Let's say I have an array that looks like this:

$myarray = array("key1"=>"val1", "key2"=>"val2");

What's the best way to iterate through this list? For regular arrays I would just do a foreach, but I'm guessing that will just give me the vals in the above example. Then I'd have to go back and look up the key for that val, which seems messy.

Any better way?
Logged

Will code for food.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #1 on: May 28, 2007, 08:59:21 AM »

I did this today Cheesy

Code:
foreach($kwCloud as $key => $value)
{
$key . ': '. $value;
}

$key refers to the name of the key
$value is its value.

If you wish to sort the array and keep the original keys use asort and arsort
Job done

BTW, I know in C++ using indexes can be slow, im guessing using string indexes is MUCH slower, anyone know how well PHP optimizes this? Or, indeed, if it does?

Logged

I am Tyler Durden
Fatty
Rookie
**
Offline Offline

Posts: 11


View Profile
« Reply #2 on: May 28, 2007, 12:12:22 PM »

Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi.

Larry Wall
Logged

No links in signatures please
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #3 on: May 28, 2007, 06:33:44 PM »

Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi.

Larry Wall

 ROFLMAO
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #4 on: May 28, 2007, 07:54:05 PM »

BTW, I know in C++ using indexes can be slow, im guessing using string indexes is MUCH slower, anyone know how well PHP optimizes this? Or, indeed, if it does?

All arrays in PHP are associative... the
  • [1] etc elements are converted to '0' and '1' before it is processed the hashing is very fast and no issue. Arrays in PHP are technically really ordered maps. Ergo there is no performance penalty for using a string rather than a numeric as an array reference. No, this is not as fast as a declared/ifed-length serial array, but they're damn fast.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!