PHP 数组 函数实例
$arr =explode(",",字符串); //explode字符串转换成数组 \n空格字符窜是空格也可以转成数组 $arr =implode(",",数组); //implode数组转换成字符 //============返回数组中指定的一列============================= array_column() //array_column获取数组里面的某个字段转成一位维数组 实例: $info= “select * from sb_htwy_articles where status=1 and picpath!='' ORDER BY createtime DESC limit 3”; //只查询最新三条 //查询二维数组 $id = implode(',',array_column($info,'id')); // 获取前三条id //======================================================= //=============计算二维数组里面的某个值总和=================== $text = array( array("dosage"=>5,"content"=>2), array("dosage"=>2,"content"=>2), array("dosage"=>2,"content"=>2), ); $arr1 = array_column($text, 'dosage'); //一维数组,拿到自己想要计算的数组值 $total = array_sum($arr1); print_r($total);die; //==================================================== //===============判断$_POST['school_id']数组里面是否有重复的值====================== count($_POST['school_id']) != count(array_unique($_POST['school_id'])) $intersection = array_diff($arr,$school_ids); //取两个数组的差集 $arr是个数组和$school_ids另一个数组 //============================================================================ //============array_filter函数 将字串转成数组并过滤其中的空值========================= $text = Array ( [0] => [1] => [2] => [3] => 11 [4] => 8, 9, 10 [5] => 5, 7 [6] => 4 [7] => 2, 3, 12 [8] => [9] => ) $a = implode(",", $text) print_r($a); //打印出来为 $b=,,,118,9,10,5,7,4,2,3,12,, $c = array_filter(implode(",", $text))); //=====================================================================================
//数组进行排序
$sort = array_column($nodes,'sort');
array_multisort($sort,SORT_ASC,$nodes);