php:输出关联数组特定范围的数据
一、php源码(将“关联数组”转化为“索引数组”,然后输出)
1 <?php 2 3 // define data structure 4 class SCOPE 5 { 6 private $scp_start=""; 7 private $scp_end=""; 8 9 public function set_start($scp_start) 10 { 11 $this->scp_start=$scp_start; 12 } 13 14 public function get_start() 15 { 16 return $this->scp_start; 17 } 18 19 public function set_end($scp_end) 20 { 21 $this->scp_end=$scp_end; 22 } 23 24 public function get_end() 25 { 26 return $this->scp_end; 27 } 28 } 29 30 31 // print array, $array_var = index array 32 function print_array($scope_var, $array_var) 33 { 34 for($st=$scope_var->get_start(); $st!=$scope_var->get_end(); $st++) 35 { 36 echo "array[$st]_out = " . $array_var[$st] . PHP_EOL; 37 } 38 } 39 40 41 42 // define relation array 43 $array_laohu=array("bg"=>"book1", 2=>"book2", 3=>"book3", 4=>"book4", "ed"=>"book5"); 44 45 // translate array; relation_array -> index_array 46 $array_translate = array_values($array_laohu); 47 48 // define scope variable and set its values. 49 $sc = new SCOPE; 50 $sc->set_start(0); 51 $sc->set_end(6); 52 53 // print array in the scope 54 print_array($sc, $array_translate); 55 56 57 58 ?>
二、运行结果