PHP中数组的各种用法

$a = 'false';
if($a){
echo '好坑';
}
输出好坑,得转换成布尔值才行哦。

in_array

$people = array("Bill", "Steve", "Mark", "David");

if (in_array("Mark", $people))
{
echo "匹配已找到";
}
else
{
echo "匹配未找到";
}

array_search

php删除一个数组元素
$a1=array("a","b","c","d","e");
array_splice($a1,array_search("d",$a1),1);
print_r($a1);
array_search
在数组中搜索键值 "d",并返回它的键名

array_splice

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
print_r(array_splice($a1,0,2,$a2));
从0开始,删除2个元素,并用$a2替换,不填$a2就是只删除
$arr1 = array(1,3, 5,7,8);
$key = array_search(3, $arr1);
if ($key !== false)
array_splice($arr1, $key, 1);
print_r($arr1);//Array ( [0] => 1 [1] => 5 [2] => 7 [3] => 8 ) 
$arr2 = array(1,3, 5,7,8);
foreach ($arr2 as $key=>$value)
{
if ($value === 3) unset($arr2[$key]);
}
print_r($arr2);//Array ( [0] => 1 [2] => 5 [3] => 7 [4] => 8 )

总结:可以看到使用array_splice()删除特定值和使用unset删除特定值是有区别的。

array_splice()函数删除的话,数组的索引值也变化了。

unset()函数删除的话,数组的索引值没有变化

$a1=array("a","b",'bei'=>'杯子',"c",'pan'=>"钢笔","e");
unset($a1['pan']);
print_r($a1);//Array ( [0] => a [1] => b [bei] => 杯子 [2] => c [3] => e )
删除某个key

current

<?php
$people = array("name"=>"Bill", 9=>"Steve", "Mark", "David");
echo current($people) . "<br>";//Bill
?>
看来current是取数组中的第一个的value
$a1=array("a","b",'bei'=>'杯子',"c",'pan'=>"钢笔","e",7=>'No7');

delByValue2($a1,'钢笔');
var_dump($a1); $a1=array("a","b","c","d","e");
delByValue2($a1,"d");
var_dump($a1); $a1=array("a","b","c","d","e");
delByValue($a1,"d");
var_dump($a1); //var_dump(delByValue($a1,"钢笔")); //foreach遍历后unset删除,
function delByValue(array &$arr, $value){
if(!is_array($arr)){
return $arr;
}
foreach($arr as $k=>$v){
if($v == $value){
unset($arr[$k]);
}
}
return $arr;
}
function delByValue2(array &$input,$value){
if(!is_array($input)){
return $input;
}
//找到是第几个元素
$index = array_search($value,array_values($input));
array_splice($input,$index,1);
return $input;
}
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'e' (length=1)
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
4 => string 'e' (length=1)

<?php
$a1=array("a","b","c",'fdsfds'=>"d","e");
array_splice($a1,array_search("d",$a1),1);
print_r($a1); echo '<Br>'; $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
print_r(array_splice($a1,0,2,$a2));
print_r($a1); echo '<br>'; $a1=array("0"=>"red","1"=>"green");
$a2=array("0"=>"purple","1"=>"orange");
array_splice($a1,1,1,$a2);
print_r($a1); echo '<br>';
$arr1 = array(1,3, 5,7,8);
$key = array_search(3, $arr1);
if ($key !== false)
array_splice($arr1, $key, 1);
print_r($arr1); echo '<br>';
$arr2 = array(1,3, 5,7,8);
foreach ($arr2 as $key=>$value)
{
if ($value === 3) unset($arr2[$key]);
}
print_r($arr2); echo '<Br>';
$a1=array("a","b",'bei'=>'杯子',"c",'pan'=>"钢笔","e");
//var_dump($a1);
//array_splice($a1,2,1);
//var_dump($a1); var_dump(array_values($a1));
var_dump(array_keys($a1)); unset($a1[1]);
unset($a1['bei']);
var_dump($a1); //var_dump(delByValue($a1,"钢笔")); //foreach遍历后unset删除,
function delByValue($arr, $value){
if(!is_array($arr)){
return $arr;
}
foreach($arr as $k=>$v){
if($v == $value){
unset($arr[$k]);
}
}
return $arr;
}
Warning: array_splice() expects parameter 2 to be long, string given in D:\Downloads\php\project\pttuicd\www\test.php on line 3
Array ( [0] => a [1] => b [2] => c [fdsfds] => d [3] => e )
Array ( [a] => red [b] => green ) Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
Array ( [0] => red [1] => purple [2] => orange )
Array ( [0] => 1 [1] => 5 [2] => 7 [3] => 8 )
Array ( [0] => 1 [2] => 5 [3] => 7 [4] => 8 )
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string '杯子' (length=6)
3 => string 'c' (length=1)
4 => string '钢笔' (length=6)
5 => string 'e' (length=1)
array
0 => int 0
1 => int 1
2 => string 'bei' (length=3)
3 => int 2
4 => string 'pan' (length=3)
5 => int 3
array
0 => string 'a' (length=1)
2 => string 'c' (length=1)
'pan' => string '钢笔' (length=6)
3 => string 'e' (length=1)
上一篇:.NET项目开发—浅谈面向对象的纵横向关系、多态入口,单元测试(项目小结)


下一篇:linux磁盘阵列 逻辑卷管理器