gpt4 book ai didi

php - 如何从数组中删除特定键? PHP

转载 作者:可可西里 更新时间:2023-10-31 23:10:16 24 4
gpt4 key购买 nike

我正在尝试从数组中删除键。

这是我打印 print_r($cats); 得到的结果

Array
(
[0] => <a href="/website/index.php/Category:All" title="Category:All">All</a> &gt; <a href="/website/index.php/Category:Computer_errors" title="Category:Computer errors">Computer errors</a> &gt; <a href="/website/index.php/Category:HTTP_status_codes" title="Category:HTTP status codes">HTTP status codes</a> &gt; <a href="/website/index.php/Category:Internet_terminology" title="Category:Internet terminology">Internet terminology</a>
[1] =>
<a href="/website/index.php/Category:Main" title="Category:Main">Main</a>
)

我正在尝试使用它从数组中删除“Main”类别

    function array_cleanup( $array, $todelete )
{
foreach( $array as $key )
{
if ( in_array( $key[ 'Main' ], $todelete ) )
unset( $array[ $key ] );

}
return $array;
}

$newarray = array_cleanup( $cats, array('Main') );

仅供引用,我是 PHP 的新手...显然我知道我犯了错误,但我已经尝试了很多东西,但它们似乎不起作用

最佳答案

Main不是数组元素,它是数组元素的一部分

function array_cleanup( $array, $todelete )
{
$cloneArray = $array;
foreach( $cloneArray as $key => $value )
{
if (strpos($value, $todelete ) !== false)
unset( $array[ $key ] ); //$array[$key] = str_replace($toDelete, $replaceWith, $value) ; // add one more argument $replaceWith to function

}
return $array;
}

编辑:

用数组

function array_cleanup( $array, $todelete )
{
foreach($todelete as $del){
$cloneArray = $array;
foreach( $cloneArray as $key => $value )
{
if (strpos($value, $del ) !== false)
unset( $array[ $key ] ); //$array[$key] = str_replace($toDelete, $replaceWith, $value) ; // add one more argument $replaceWith to function

}
}
return $array;
}

$newarray = array_cleanup( $cats, array('Category:Main') );

关于php - 如何从数组中删除特定键? PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5202068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com