2010-01-25 10 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

특정 요소를 검색하는 다차원 배열 $ users가 있습니다.이 요소가 발견되면 $ users에서 제거하려고합니다. 그렇습니까? 네가 묻고있는 것을 이해하려고 노력한다. – Gordon

+0

예, 정확히 원하는 것 – JPro

답변

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

다른 방법은 찾고 있던 값으로 모든 키를 삭제하는 것입니다.

$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

0

패스 $ 사용자가 참조 array_searchRecursive하기 ($ 건초 더미에 '&을'추가) :

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

(도전적으로 권장되지는 않지만) 완전성에 대해
/** 
* 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; 
} 

는 여기에 평가 버전입니다

및 각 return 문 바로 앞에있는 array_searchRecursive에서

 
unset($haystack[$key]);