PHP 5 Array 函数
数组的取值
二维数组取值
例:
array(2) { [0]=> array(2) { ["msg_type"]=> string(1) "0" ["msg_content"]=> string(39) "中华民族" } [1]=> array(2) { ["msg_type"]=> string(1) "0" ["msg_content"]=> string(39) "中国梦" } }
获取方式:
$msg_content[0]['msg_content'] Cannot use object of type stdClass as array in
二维对象数组取值
例:
Array ( [0] => stdClass Object ( [id] => 1 [title] => 青山 [size] => 300*150 [pic] => ./upload/123.jpg [state] => 0 ) [1] => stdClass Object ( [id] => 2 [title] => 绿水 [size] => 300*150 [pic] => ./upload/456.jpg [state] => 0 ) )
获取方式:
$pic[0]->title
向数组内添加元素
普通
$dataList = array(); array_push($dataList, array( 'gmt_create' => $value['gmt_create'], 'reported_user_id' => $value['reported_user_id'], 'reported_user_name' => $reportedUserName[0]['snickname'], 'report_type' => $value['report_type'], 'report_source' => $value['report_source'], 'report_info_id' => $value['report_info_id'], 'report_info_type' => $reportInfoType, 'report_info' => $reportInfo, 'report_user_name' => $reportUserName[0]['snickname'] )); var_dump($dataList);
动态向数组内追加元素
$arr = array(); for($i=0;$i<10;$i++){ array_push($arr, $i); // 键值对形式直接: $array['键'] = $值 $array['键'] = $值 $array['键'] = $值 'report_info_id' => $value['report_info_id'], 'report_info_type' => $reportInfoType, } var_dump($arr);
打印数组的函数:print_r()函数和var_dump()函数
var_dump()函数
<?php $arr_test = array(1,2,3); var_dump($arr_test); ?>
运行该例子输出:
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
print_r()函数
<?php $arr_test =array(1, 2, 3); print_r($arr_test); ?>
运行该例子输出:
Array ( [0] => 1 [1] => 2 [2] => 3 )
var_dump()函数同print_r()函数用法一样。不过var_dump()函数功能比print_r()更强大,可以同时打印多个变量且给出变量的类型信息。
根据初始位置和下标截取数组
$canFakeListTwo = array_slice($canFakeList, $random-0, $cnt - $approveCntOld);
Q.E.D.