PHP教程

PHP常用函数整理

本文主要是介绍PHP常用函数整理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

PHP常用函数整理


array_since()

从选择的下标开始取出

$arr = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

$arr1 = array_slice($arr,2); # 從arr的下標2開始取出

foreach ($arr1 as $key=>$value){
echo $key.'='.$value.'<br/>';
}

输出

c=3<br/>d=4<br/>e=5<br/>

array_unshift

用于向数组插入新元素。新数组的值将被插入到数组的开头。

$classResult = $this->classService->detailInfo(array_column($class, 'class'), 0, 'id,name,grade');

$list = [];
$list['id'] = 0;
$list['name'] = "年级榜首(仅展示前200名)";
$list['grade'] = $studentInfo['grade_id'];
$list['exam_id'] = $where['exam_id'];
$list['exam_subject_id'] = $examSubject;

array_unshift($classResult, $list);

输出

Array
(
[0] => Array
(
[id] => 0
[name] => 年级榜首(仅展示前200名)
[grade] => 3
[exam_id] => 95
[exam_subject_id] => 4
)
[1] => Array
(
[id] => 3
[name] => 高三3班
[grade] => 3
[exam_id] => 95
[exam_subject_id] => 4
)
)

array_push

array_push() 函数向数组尾部插入一个或多个元素。

        $classResult = $this->classService->detailInfo(array_column($class, 'class'), 0, 'id,name,grade');

        $list = [];
        $list['id'] = 0;
        $list['name'] = "年级榜首(仅展示前200名)";
        $list['grade'] = $studentInfo['grade_id'];
        $list['exam_id'] = $where['exam_id'];
        $list['exam_subject_id'] = $examSubject;

        array_push($classResult, $list);

输出

Array
(
[0] => Array
(
[id] => 3
[name] => 高三3班
[grade] => 3
[exam_id] => 95
[exam_subject_id] => 4
)

[1] => Array
(
[id] => 0
[name] => 年级榜首(仅展示前200名)
[grade] => 3
[exam_id] => 95
[exam_subject_id] => 4
)
)

array_column

返回输入数组中某个单一列的值。

$class = $this->baseAchievementService->accordingExamSubjectIdAndExamIdAndGradeIdGetGradeList(['exam_subject_id' => $examSubject, 'exam_id' => $where['exam_id'], 'grade' => $studentInfo['grade_id']], "DISTINCT class");

print_r($class);
print_r("===array_column===");
print_r(s($class, 'class'));

输出

Array
(
[0] => Array
(
[class] => 3
)
)
===array_column===
Array
(
[0] => 3
)

array_pop

删除数组中的最后一个元素

$a=array("red","green","blue");
array_pop($a);
print_r($a);

输出

Array ( [0] => red [1] => green )

array_map

函数返回用户自定义函数作用后的数组。回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致。

function myfunction($v) {
if ($v === "Dog") {
    return "Fido";
}
return $v;
}
 
$a = array("Horse", "Dog", "Cat");
print_r(array_map("myfunction", $a));

输出

Array ( [0] => Horse [1] => Fido [2] => Cat )

array_merge

把两个数组合并为一个数组

$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));

输出

Array
(
[a] => red
[b] => yellow
[c] => blue
)

array_merge_recursive

把两个数组合并为一个数组

$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));

输出

Array ( [a] => red [b] => Array ( [0] => green [1] => yellow ) [c] => blue )

array_filter

用回调函数过滤数组中的元素

function test_odd($var)
{
return($var & 1);
}
 
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));

输出

Array ( [3] => 3 )

array_shift

删除数组中的第一个元素(red),并返回被删除元素的值

$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
print_r ($a);

输出

red 
Array ( [b] => green [c] => blue )

array_search

在数组中搜索键值 “red”,并返回它的键名

$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);

输出

a

博客来源:雨夜的博客

这篇关于PHP常用函数整理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!