我试图从PHP中的多维数组中删除一个元素。这是代码:
<?php
$tset = "ST3";
$gettc = "gettingtc1";
$getbid = "gettingbid1";
$getresultsid = "gettingresid1";
$users[$tset] = array();
$users[$tset][] = array( "testcase"=>"$gettc",
"buildid"=>"$getbid",
"resultsid"=>"$getresultsid"
);
$tset = "TEJ";
$gettc = "ggettingtc2";
$getbid = "gettingbid2";
$getresultsid = "gettingresid2";
$users[$tset][] = array( "testcase"=>"$gettc",
"buildid"=>"$getbid",
"resultsid"=>"$getresultsid"
);
$tset = "ST3";
$gettc = "ggettingtc12";
$getbid = "gettingbid13";
$getresultsid = "gettigresid14";
$users[$tset][] = array( "testcase"=>"$gettc",
"buildid"=>"$getbid",
"resultsid"=>"$getresultsid"
);
foreach ($users as $val => $yy)
{
echo "For $val the array is :";
foreach($yy as $uy)
{
echo $uy['testcase'].$uy['buildid'].$uy['resultsid'];
}
echo '<br>';
}
$ser = "gettingresid1";
$to = array_searchRecursive($ser,$users);
if($to <> 0)
{
print_r($to);
}
else
{
echo "not";
}
function array_searchRecursive( $needle, $haystack, $strict=true, $path=array() )
{
if( !is_array($haystack) ) {
return false;
}
foreach( $haystack as $key => $val ) {
if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
$path = array_merge($path, array($key), $subPath);
return $path;
} elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
$path[] = $key;
return $path;
}
}
return false;
}
?>
我遇到的问题是:$to
保存包含搜索元素的数组。但是应该从原始数组$to
中删除结果$users
。
任何帮助。
感谢。
答案 0 :(得分:2)
我认为你想使用unset()
//assuming $to contains the key in $users that needs to be removed
//from the array
unset($users[$to]);
但由于$to
包含元素的键数组而不是元素的单个键,因此您需要编写自己的函数来执行所需的操作。
执行所需操作的函数位于下方,并将删除具有$ keys中地址的给定数组元素。
/**
* Removes the element with the keys in the array $keys
*
* @param haystack the array that contains the keys and values
* @param keys the array of keys that define the element to remove
* @return the new array
*/
function array_remove_bykeys( array $haystack, array $keys ){
//check if element couldn't be found
if ( empty($keys) ){
return $haystack;
}
$key = array_shift($keys);
if ( is_array($haystack[$key]) ){
$haystack[$key] = array_remove_bykeys($haystack[$key], $keys);
}else if ( isset($haystack[$key]) ){
unset($haystack[$key]);
}
return $haystack;
}
另一种方法是删除所有具有您要查找的值的键。
/**
* Removes all elements from that array that has the value $needle
* @param $haystack the origanal array
* @param $needle the value to search for
* @return a new array with the value removed
*/
function array_remove_recursive( array $haystack, $needle ){
foreach( $haystack as $key => $value ) {
if( is_array($value) ) {
$haystack[$key] = array_remove_recursive($value, $needle);
} else if ( $value === $needle ){
unset($haystack[$key]);
}
}
return $haystack;
}
为了完整性(虽然不推荐),这是一个评估版本:
$keys = array(...);//an array of keys
//$arr is the variable name that contains the value that you want to remove
eval ('unset($arr[\''.implode('\'][\'', $keys).'\']);');
答案 1 :(得分:0)
通过引用将$ users传递给array_searchRecursive(将'&amp;'添加到$ haystack):
function array_searchRecursive( $needle, &$haystack, $strict=true, $path=array()
然后在array_searchRecursive中,就在每个return语句之前:
unset($haystack[$key]);