php数组值 增加 删除 查找操作方法详解_PHP教程

WBOY
Release: 2016-07-13 16:57:08
Original
843 people have browsed it

在php数组中分为数组值与数组key,下面小编来给大家总结一下在php中数组值常用的操作方法包括有:数组中加入数值、判断 数组中的数值、删除特定数组值等有需要的同学可参考。

php删除特定数组值

首先

 代码如下 复制代码
var_dump($context['linktree']);

得到

 代码如下 复制代码
array(3) {
[0]=>
array(2) {
["url"]=>
string(52) “http://127.0.0.1/testforum.cityofsteam.com/index.php”
["name"]=>
string(28) “City of Steam Official Forum”
}
[1]=>
array(2) {
["url"]=>
string(55) “http://127.0.0.1/testforum.cityofsteam.com/index.php#c1″
["name"]=>
string(28) “City of Steam Official Forum”
}
[2]=>
array(2) {
["url"]=>
string(62) “http://127.0.0.1/testforum.cityofsteam.com/index.php?board=4.0″
["name"]=>
string(12) “Announcement”
}
}

我要去掉中间那个。

用:unset($context['linktree']['1']);

结果:

 代码如下 复制代码

array(2) {
[0]=>
array(2) {
["url"]=>
string(52) “http://127.0.0.1/testforum.cityofsteam.com/index.php”
["name"]=>
string(28) “City of Steam Official Forum”
}
[2]=>
array(2) {
["url"]=>
string(62) “http://127.0.0.1/testforum.cityofsteam.com/index.php?board=4.0″
["name"]=>
string(12) “Announcement”
}
}

就少了一个[1]

让这中间的1自动编号:

 代码如下 复制代码


Array ( [0] => apple [1] => banana [3] => dog )

但是这种方法的最大缺点是没有重建数组索引,就是说,数组的第三个元素没了。
经过查资料后,原来PHP提供了这个功能,只不过很间接。这个函数是array_splice()。
为了使用方便,我封装成了一个函数,方便大家使用:

 代码如下 复制代码

function array_remove(&$arr, $offset)

{
 
array_splice($arr, $offset, 1);

}

$arr = array('apple','banana','cat','dog');
 
array_remove($arr, 2);
 
print_r($arr);

?>

经过测试可以知道,2的位置这个元素被真正的删除了,并且重新建立了索引。

程序运行结果:

 代码如下 复制代码

Array ( [0] => apple [1] => banana [2] => dog )


php判断 数组中的数值


有专门的函数,不要用for循环,系统函数能实现快速搜索:

in_array
(PHP 4, PHP 5)

in_array — 检查数组中是否存在某个值

说明
bool in_array ( mixed $needle, array $haystack [, bool $strict] )

在 haystack 中搜索 needle,如果找到则返回 TRUE,否则返回 FALSE。

如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。

注意: 如果 needle 是字符串,则比较是区分大小写的。

注意: 在 PHP 版本 4.2.0 之前,needle 不允许是一个数组。

例 292. in_array() 例子

 代码如下 复制代码
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>

第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:

Got Irix

例 293. in_array() 严格类型检查例子

 代码如下 复制代码

$a = array('1.10', 12.4, 1.13);

if (in_array('12.4', $a, true)) {
    echo "'12.4' found with strict checkn";
}
if (in_array(1.13, $a, true)) {
    echo "1.13 found with strict checkn";
}
?>
上例将输出:

1.13 found with strict check

例 294. in_array() 中用数组作为 needle

 代码如下 复制代码

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
    echo "'ph' was foundn";
}
if (in_array(array('f', 'i'), $a)) {
    echo "'fi' was foundn";
}
if (in_array('o', $a)) {
    echo "'o' was foundn";
}
?>
上例将输出:

  'ph' was found
  'o' was found


向一个数组中加入数值

我们可以通过函数来实现,将一个或多个元素插入到数组中去,也可以直接添加进去。
(1)向数组中直接添加数据,新元素的下标是从原数组下标最大值之后开始的。
(2)array_unshift()函数在数组的开头添加一个或多个元素。
语法如下:
int array_unshift ( array &array, mixed var [,mixed ...]) ;
array_unshift()将传入的元素插入到array数组的开头。元素是作为整体被插入的,传入元素将保持同样的顺序。所有的数值键名将从0开始重新计数,文字键名保持不变。
(3)array_push()函数将一个或多个单元添加到数组的末尾。
语法:
int array_push ( array &array, mixed var [, mixed ...]) ;
array_push()将array当成一个栈,并将传入的变量添加到array的末尾。该函数返回数组新的单元总数。向数组中添加数据的示例如下。
示例:

 代码如下 复制代码
$shili = array (“1″,”2″,”3″,”4″) ;
$shili[]=5 ;                            //直接添加数据
print_r ( $shili ) ;
echo “
” ;
$shili2 = array (“m”,”n”) ;
array_unshift ($shili2,”o”,”p”) ;          //添加元素于数组的开头
print_r ( $shili2 ) ;
echo “
” ;
$shili3 = array (“Php”) ;
array_push ($shili3, “MySQL”,”Apache”) ; //添加元素于数组的末尾
print_r ($shili3) ;
?>
结果为:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Array ( [0] => o [1] => p [2] => m [3] => n )
Array ( [0] => Php [1] => MySQL [2] => Apache )

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631553.htmlTechArticle在php数组中分为数组值与数组key,下面小编来给大家总结一下在php中数组值常用的操作方法包括有:数组中加入数值、判断 数组中的数值、删...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!