PHP常用實用函數_PHP教程

來源:互聯網
上載者:User
1. 對於foreach其實 用$key=>$value和$i => $g 是效果一樣的,並不僅限於key value

echo "
\n";

$arr = array("one", "two", "three");

foreach ($arr as $i => $g) {

echo "Key: $i; Value: $g
\n";

}

?>

2. array_fill ()函數

用給定的值填充數組

$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人生

http://www.bkjia.com/PHPjc/478354.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478354.htmlTechArticle1. 對於foreach其實 用$key=$value和$i = $g 是效果一樣的,並不僅限於key value ?php echo br /\n; $arr = array(one, two, three); foreach ($arr as $i = $g) { echo Key:...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.