欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP常用实用函数_PHP教程

程序员文章站 2022-06-11 10:22:02
...
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人生

www.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:...