如何在数组中查找值并使用PHP数组函数将其删除
如何在数组中找到一个值以及如何删除它。 如果有任何php内置数组函数可以做到这一点。 删除后,我需要顺序索引顺序。 任何人都知道,请帮助我。
9个解决方案
93 votes
$my_array = array('sheldon', 'leonard', 'howard', 'penny');
$to_remove = array('howard');
$result = array_diff($my_array, $to_remove);
?>
Peter answered 2020-01-12T05:17:24Z
91 votes
要搜索数组中的元素,可以使用array_search函数,而要删除数组中的元素,可以使用unset函数。 例如:
$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]了解更多与数组相关的功能。
mohitsoni answered 2020-01-12T05:17:08Z
14 votes
您需要首先找到数组的键,这可以使用array_search()完成
完成后,使用unset()
$array = array( 'apple', 'orange', 'pear' );
unset( $array[array_search( 'orange', $array )] );
?>
Kerry Jones answered 2020-01-12T05:17:48Z
8 votes
万一您想使用任何提及的代码,请注意,当在“干草堆”中未找到“针头”时,array_search返回FALSE,因此这些示例将取消设置第一个(零索引)项目。 使用此代替:
$haystack = Array('one', 'two', 'three');
if (($key = array_search('four', $haystack)) !== FALSE) {
unset($haystack[$key]);
}
var_dump($haystack);
上面的示例将输出:
Array
(
[0] => one
[1] => two
[2] => three
)
那很好!
chyno answered 2020-01-12T05:18:17Z
4 votes
此解决方案是@Peter解决多个事件出现的解决方案和@chyno解决方案删除第一次出现的解决方案的组合。 这就是我正在使用的。
/**
* @param array $haystack
* @param mixed $value
* @param bool $only_first
* @return array
*/
function array_remove_values(array $haystack, $needle = null, $only_first = false)
{
if (!is_bool($only_first)) { throw new Exception("The parameter 'only_first' must have type boolean."); }
if (empty($haystack)) { return $haystack; }
if ($only_first) { // remove the first found value
if (($pos = array_search($needle, $haystack)) !== false) {
unset($haystack[$pos]);
}
} else { // remove all occurences of 'needle'
$haystack = array_diff($haystack, array($needle));
}
return $haystack;
}
也可以在这里看看:PHP数组按值删除(不是键)
algorhythm answered 2020-01-12T05:18:42Z
4 votes
一个未提及的函数是array_values。您只需要传递一个回调函数,该函数将每个元素作为参数并返回true或false(如果该元素应该保留或删除)。 这也具有删除重复值的好处。
您可以像这样使用它:
$myArray = array('one', 'two', 'deleteme', 'three');
$output = array_filter($myArray, function($val) { return $val != 'deleteme'; });
如果要重新索引数组,可以将结果传递给array_values,如下所示:
$output = array_values($output);
Kodos Johnson answered 2020-01-12T05:19:11Z
1 votes
首先,就像其他人提到的那样,您将使用“ array_search()”和“ unset()”方法,如下所示:-
$arrayDummy = array( 'aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff', 'gggg' );
unset( $arrayDummy[array_search( 'dddd', $arrayDummy )] ); // Index 3 is getting unset here.
print_r( $arrayDummy ); // This will show the indexes as 0, 1, 2, 4, 5, 6.
?>
现在,要重新索引同一数组,而不对任何数组值进行排序,则需要使用“ array_values()”方法,如下所示:
$arrayDummy = array_values( $arrayDummy );
print_r( $arrayDummy ); // Now, you will see the indexes as 0, 1, 2, 3, 4, 5.
?>
希望能帮助到你。
Knowledge Craving answered 2020-01-12T05:19:40Z
0 votes
这就是我要做的:
$terms = array('BMW', 'Audi', 'Porsche', 'Honda');
// -- purge 'make' Porsche from terms --
if (!empty($terms)) {
$pos = '';
$pos = array_search('Porsche', $terms);
if ($pos !== false) unset($terms[$pos]);
}
Mike Q answered 2020-01-12T05:19:59Z
-1 votes
为了查找和删除数组中值的多个实例,我使用了以下代码
$list = array(1,3,4,1,3,1,5,8);
$new_arr=array();
foreach($list as $value){
if($value=='1')
{
continue;
}
else
{
$new_arr[]=$value;
}
}
print_r($new_arr);
Brijesh Mishra answered 2020-01-12T05:20:19Z