Bootstrap

PHP基础

<?php

$student = array("张三","李四","王五","赵六","孙七","陈八");

//取数组长度

$len = count($student);

echo $len;

echo $student[0]."<br>";

//修改元素

$student[1] = "玉玉";

print_r($student);

echo "<br>";

//删除最后一个

array_pop($student);

print_r($student);

//随机取值

$index= array_rand($student);

echo "<br>";

echo $student[$index];

echo "<br>";

/*或者

$index = rand(0,count($student)-1);

echo "<br>";

echo $student[$index];

*/ //数组去重

$score = array("10","20","30","40","20","30","40");

print_r($score);

$new_arry = array_unique($score);

echo "<br>";

print_r($new_arry);

echo "<br>";

//数组遍历

for ($i=0;$i<count($student);$i++){

echo $student[$i]."<br>";

}

//关联数组

$student_1 = array('name'=>'张三','id'=>'1','addr'=>'北京');

$student_2 = array('name'=>'李四','id'=>'2','addr'=>'上海');

$student_3 = array('name'=>'呜呜','id'=>'2','addr'=>'成都');

echo "<br>"; $student_1['name'] = "哈哈";

//遍历数组的value

//或者使用 array_values end()取最后一个

foreach($student_1 as $stu){

echo $stu; echo "<br>";

}

//遍历数组

key array_key_last()

$keys = array_keys($student_1);

foreach($keys as $key){ echo $key."-----";

echo $student_1[$key]; echo"<br>"; }

//字符串转数组

$source = 'taobao.com.cn.net.edu';

$myarray = explode('.',$source);

echo "<br>"."----";

print_r($myarray);

//数组转字符串 $grade = array(1,2,3,4,5,6,7,8,9);

$string_gradre = implode(',',$grade); print_r(($string_gradre));

?>

;