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

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 0.93 KB  |  hits: 18  |  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 how to loop through an array and grab specific parts
  2. Array (
  3.  [0] => 1
  4.  [1] => Player1
  5.  [2] =>
  6.  [3] => 1
  7.  [4] => 0
  8.  [5] => 0
  9.  [6] => 0
  10.  [7] => 0
  11.  [8] => 1
  12.  [9] => Player2
  13.  [10] =>
  14.  [11] => 1
  15.  [12] => 0
  16.  [13] => 0
  17.  [14] => 0
  18.  [15] => 0
  19. )
  20.        
  21. $playerNames = array_diff($array, array('', 0, 1));
  22.        
  23. $playerSize = 8;
  24. $playerFields = array('_1', 'name', '_3', '_4', '_5', '_6', '_7', '_8');
  25. $players = array_chunk($array, $playerSize);
  26.  
  27. foreach($players as &$player)
  28. {
  29.     $player = (object) array_combine($playerFields, $player);
  30. }
  31. unset($player);
  32.        
  33. printf("%d Player(s):n", count($players));
  34.  
  35. foreach($players as $i => $player)
  36. {
  37.     printf("#%d: %sn", $player->name);
  38. }
  39.        
  40. $players = array();
  41. foreach($array as $player){
  42.     if(!empty($player) && !is_numeric($player){
  43.         $players[]=$player;
  44.     }
  45. }
  46.  
  47. var_dump($players);
  48.        
  49. $i= 1;
  50. while ($i < count($return_var)) {
  51.     $name = $return_var[$i];
  52.     // do something w/ name
  53.     $i += 8;
  54. }