Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.27 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP - foreach loop
  2. $idx = 0;
  3.  
  4. foreach($this->categories as $category)
  5. {
  6.   echo "n".($idx++ ? '| ' : '').'<a href="'.KM_Helpers::getCategoryURL($category).'">'.$category['name'].'</a>';
  7. }
  8.        
  9. $myarray = array(2, 8, 21, 22);
  10.        
  11. $idx = 0;
  12. $myarray = array(2, 8, 21, 22);
  13. foreach($this->categories as $category)
  14. {
  15.     if (!in_array($category['id'], $myarray)) {
  16.         continue; // skip it if the id isn't in your array of accceptable IDs
  17.     }
  18.     echo "n".($idx++ ? '| ' : '').'<a href="'.KM_Helpers::getCategoryURL($category).'">'.$category['name'].'</a>';
  19. }
  20.        
  21. foreach ($myarray as $id)
  22. {
  23.   $category = $this->categories[$id];
  24.   echo "n".($idx++ ? '| ' : '') .
  25.     '<a href="' . KM_Helpers::getCategoryURL($category) . '">' .
  26.      $category['name'] . '</a>';
  27. }
  28.        
  29. $idx = 0;
  30. $myarray = array(2, 8, 21, 22);
  31.  
  32. foreach($this->categories as $category)
  33. {
  34.     if(in_array($category['id'], $myarray)
  35.     {
  36.         echo "n".($idx++ ? '| ' : '').'<a href="'.KM_Helpers::getCategoryURL($category).'">'.$category['name'].'</a>';
  37.     }
  38. }
  39.        
  40. $ids= array(2, 8, 21, 22);
  41.  
  42. $idx = 0;
  43. foreach($this->categories as $id => $category){
  44.     if (in_array($category['id'] , $ids)) {
  45.         echo "n".($idx++ ? '| ' : '').'<a href="'.KM_Helpers::getCategoryURL($category).'">'.$category['name'].'</a>';
  46.     }
  47. }