我想從PHP中的多維數組中移除一個元素。下面是代碼:從多維數組中刪除一個元素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
中刪除。
任何幫助。
謝謝。
你有一個多維數組$ user,你搜索一個特定的元素,如果找到這個元素,你想從$ users中刪除它,對吧?只是想明白你在問什麼。 – Gordon 2010-01-25 12:16:39
是的,那正是我想要的 – JPro 2010-01-25 12:20:40