繁体   English   中英

从多维数组php中删除一个元素

[英]removing an element from multidimentional array 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删除。

任何帮助。

谢谢。

我认为您想使用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).'\']);');

通过引用将$ users传递给array_searchRecursive(在$ haystack中添加“&”):

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

然后在每个return语句之前的array_searchRecursive中:

unset($haystack[$key]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM