数组有时在处理临时数据时,很有用,二维数组有优势。关于数据新建与赋值如下:
$arr[0]['name'] = 'zhangsan'; $arr[0]['chinese'] = '89'; $arr[0]['math'] = '95'; $arr[1]['name'] = 'lisi'; $arr[1]['chinese'] = '91'; $arr[1]['math'] = '86';
也可这样,与上者等价,完全一样。
$arr= array( array('name'=>'zhangsan','chinese'=>'89','math'=>'95'), array('name'=>'lisi','chinese'=>'91','math'=>'86') );
数组内容是:
Array( [0] => Array( [name] => zhangsan [chinese] => 89 [math] => 95 ) [1] => Array( [name] => lisi [chinese] => 91 [math] => 86 ) )
访问方法:
echo $arr[0]['name'].'同学的数学考了'.$arr[0]['math'].'分'; //输出,zhangsan同学的数学考了95分
$arr=array(); $arr[a]=array(); array_push($arr[a],1); array_push($arr[a],2); array_push($arr[a],3); $arr[b]=array(); $arr[b]=2; $arr[c]=3; //结果如下: Array( [a] => Array( [0] => 1 [1] => 2 [2] => 3 ) [b] => 2 [c] => 3 )