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

 
Back to top
php/snippets.txt · Last modified: 2009/07/09 20:05 (external edit)
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0