如何在数组中找到值并通过使用PHP数组函数将其删除 [英] How to find a value in array and remove it by using PHP array functions

查看:198
本文介绍了如何在数组中找到值并通过使用PHP数组函数将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找数组中存在的值以及如何删除它。如果任何PHP内置数组的功能来做到这一点。删除后,我需要顺序索引顺序。任何机构知道请帮助我。

How to find a value exist in an array and how to remove it. If any php builtin array functions for doing this. After removing I need the sequential index order. any body knows please help me.

推荐答案

要搜索数组中的元素,可以使用 array_search 函数并从数组中移除一个元素,您可以使用 unset 函数。例如:

To search an element in an array, you can use array_search function and to remove an element from an array you can use unset function. Ex:

<?php
$hackers = array ('Alan Kay', 'Peter Norvig', 'Linus Trovalds', 'Larry Page');

print_r($hackers);

// Search
$pos = array_search('Linus Trovalds', $hackers);

echo 'Linus Trovalds found at: ' . $pos;

// Remove from array
unset($hackers[$pos]);

print_r($hackers);
?>

您可以参考: http://www.php.net/manual/en/ref.array.php 以获取更多与数组相关的功能。

You can refer: http://www.php.net/manual/en/ref.array.php for more array related functions.

这篇关于如何在数组中找到值并通过使用PHP数组函数将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆