2010-01-25 118 views
0

我想從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中刪除。

任何幫助。

謝謝。

+0

你有一個多維數組$ user,你搜索一個特定的元素,如果找到這個元素,你想從$ users中刪除它,對吧?只是想明白你在問什麼。 – Gordon 2010-01-25 12:16:39

+0

是的,那正是我想要的 – JPro 2010-01-25 12:20:40

回答

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; 
} 

爲了完整(雖然挑釁不推薦),這裏是一個EVAL版本:

$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).'\']);'); 
+0

警告:在C:\ xampplite \ htdocs \ scripts \ multi_arrays.php中未設置的非法偏移類型在65行 – JPro 2010-01-25 12:34:23

0

通$用戶參考array_searchRecursive(添加 '&' 至$草堆):

 
function array_searchRecursive($needle, &$haystack, $strict=true, $path=array() 

然後在array_searchRecursive中,在每個return語句之前:

 
unset($haystack[$key]);