<?php
header('Content-type:text/html;charset=utf-8');
//php遍历二位数组
$arr = [
['name0'=>'孙悟空', 'age'=>33,'sex'=>'男'],
['name1'=>'白龙马', 'age'=>33,'sex'=>'男'],
['name2'=>'猪八戒', 'age'=>33,'sex'=>'男'],
['name3'=>'唐僧', 'age'=>33,'sex'=>'男'],
['name4'=>'沙僧', 'age'=>33,'sex'=>'男'],
['name5'=>'牛魔王', 'age'=>33,'sex'=>'男']
];
$str_tab = "<table border='1' ruls='all'>";
foreach($arr as $k => $v){ //$k ==>name0 $v ==>'孙悟空', 'age'=>33,'sex'=>'男'
$str_tab .= "<tr>";
foreach($v as $vo){ //$vo ==> (孙悟空 33 男)
$str_tab .= "<td>$vo</td>";
}
$str_tab .= "</tr>";
}
$str_tab .="</table>";
echo $str_tab."<hr>";
//$arr = array(1,2,3,4,5);
$arr = [1,2,3,4,5];
foreach($arr as $k => $v){
echo current($arr),'---',$k,'-------',$v."<br/>"; //current ==> 返回数组中的当前单元
}
//echo current($arr);
var_dump (current($arr));
echo "<hr/>";
$arr = [1,2,3,4,5];
//$v = &$arr[0];
//$v =1;
foreach($arr as $k => &$v){ //& ==> 物理地址(引用传值) 是将物理地址赋值给$v
$v = $v*2;
}
echo "<pre>";
var_dump($arr);
echo "<hr/>";
$arr = [1,2,3,4,5];
//$v = &$arr[0];
//$v =1;
foreach($arr as $k => $v){ //1 直接赋值给 $v,
$v = $v*2;
}
echo "<pre>";
var_dump($arr); |
|