Snippets

Saves the world!

Hahuaehuae isso era eu qurendo programar Ruby….. sem nem conhece-lo…

http://wiki.nofxx.com/ruby

Um link ^ pro que presta, por favor

LIXO

Traverse associative array

reset($fruit);
while (list($key, $val) = each($fruit)) {
  echo "$key => $val\n";
}

Or easier with a foreach loop e.g.;

// Foreach should begin be resetting
foreach ( $fruit as $key => $value ) {
  echo "$key => $value\n";
}

Sometime you need to preverse references (e.g. when it's an array of objects in PHP4), which can be done efficiently like;

foreach ( array_keys($fruit) as $key ) {
   echo "$key => {$fruit[$key]}\n";
}

Other functions like file can also be used nicely with foreach e.g.;

foreach ( file('somefile.txt') as $line ) {
   echo "$line\n";
}

Sources

http://wiki.splitbrain.org/phpsnippets