1. 對於foreach其實 用$key=>$value和$i => $g 是效果一樣的,並不僅限於key value
<?php
echo "<br />\n";
$arr = array("one", "two", "three");
foreach ($arr as $i => $g) {
echo "Key: $i; Value: $g<br />\n";
}
?>
2. array_fill ()函數
用給定的值填充數組
<?php
$a = array_fill(5, 6, 'banana');
print_r($a);
?>
輸出:
Array ( [5] => banana [6] => banana [7] => banana [8] => banana [9] => banana [10] => banana )
3. 打印數組(PHP模式和JSON模式)
$data = array(
'firstname'=>'Tom',
'lastname'=>'Smith',
'age'=>40
);
print_r($data);
echo json_encode($data);
輸出:
Array ( [firstname] => Tom [lastname] => Smith [age] => 40 )
{"firstname":"Tom","lastname":"Smith","age":40}
4. print_r($data,true);這樣才可以把數組寫到日志裡www.2cto.com
$a = array ("a", "b", "c");
$c= print_r($a,true);
$logfile = "t.txt";
error_log("{$c}\r\n\r\n",3,$logfile);
摘自 河大李信的 Crazy Coding人生