PHP:从嵌套数组中删除值

PHP:从嵌套数组中删除值,php,Php,我正在从嵌套数组中提取重复的值。我想从$bigarray中删除这些提取项。你能给我一些想法吗 $bigarray = array(

我正在从嵌套数组中提取重复的值。我想从$bigarray中删除这些提取项。你能给我一些想法吗

$bigarray = array(                                                                                                                                                                                                                                                          
    "430" => array('milk', 'turky', 'apple'),
    "433" => array('milk', 'apple', 'orange', 'england'),
    "444" => array('milk', 'apple', 'orange', 'spain')
);
$intersected = null;
foreach ($bigarray as $arr) {
  $intersected = $intersected ? array_intersect($arr, $intersected) : $arr;
  if (!$intersected) {
    break; // no reason to continue
  }
}


foreach ($intersected as $inter){
  foreach ($bigarray as $arr) { 
    foreach ($arr as $value=>$key) {
      if ($key ==  $inter){
        unset($arr[$value]);
      }
    }
  //print_r($arr);
  }
}
print_r($bigarray );

这就是你要找的吗

foreach($bigarray as $id => $arr)
    $bigarray[$id] = array_unique($arr);

这就是你要找的吗

foreach($bigarray as $id => $arr)
    $bigarray[$id] = array_unique($arr);
您应该查看,因为它会将两个数组合并在一起,并且只保留一个副本。从手册中:

如果输入数组具有相同的字符串键,则后面的值 因为该键将覆盖上一个键。但是,如果 如果包含数字键,则后面的值不会覆盖原始值 值,但将被追加

听起来像是家庭作业,问题不是很清楚,所以我现在只能提供这些。

你应该看看,因为它将两个数组合并在一起,只保留一个副本。从手册中:

如果输入数组具有相同的字符串键,则后面的值 因为该键将覆盖上一个键。但是,如果 如果包含数字键,则后面的值不会覆盖原始值 值,但将被追加


听起来像是家庭作业,问题不是很清楚,所以我现在只能提供这些。

我不完全理解你的问题,但在你的数组中使用array_unique(),我得到了以下输出:

array(1) {
  [430]=>
  array(3) {
    [0]=>
    string(4) "milk"
    [1]=>
    string(5) "turky"
    [2]=>
    string(5) "Apple"
  }
}

也许这是一种获得你想要的东西的方法。

我不完全理解你的问题,但是在你的数组中使用array_unique()我得到了以下输出:

array(1) {
  [430]=>
  array(3) {
    [0]=>
    string(4) "milk"
    [1]=>
    string(5) "turky"
    [2]=>
    string(5) "Apple"
  }
}
function array_unique_nested($arr=array(),$matched=array(),$cm=false){
foreach($arr as $i=>$v){
if (is_array($v)) {$arr[$i]=array_unique_nested($v,$matched,false);
$matched=array_unique_nested($v,$matched,true); continue;}
if (in_array($v,$matched)) {unset($arr[$i]);continue;}
$matched[]=$v;}
if ($cm) return $matched;
else return $arr;}
也许这是一种获得你想要的东西的方式

function array_unique_nested($arr=array(),$matched=array(),$cm=false){
foreach($arr as $i=>$v){
if (is_array($v)) {$arr[$i]=array_unique_nested($v,$matched,false);
$matched=array_unique_nested($v,$matched,true); continue;}
if (in_array($v,$matched)) {unset($arr[$i]);continue;}
$matched[]=$v;}
if ($cm) return $matched;
else return $arr;}
如果这不起作用,那么来自的这个代码段应该会起作用

if( !function_exists( 'array_flat' ) )
{
    function array_flat( $a, $s = array( ), $l = 0 )
{
    # check if this is an array
    if( !is_array( $a ) )                           return $s;

    # go through the array values
    foreach( $a as $k => $v )
    {
        # check if the contained values are arrays
        if( !is_array( $v ) )
        {
            # store the value
            $s[ ]       = $v;

            # move to the next node
            continue;

        }

        # increment depth level
        $l++;

        # replace the content of stored values
        $s              = array_flat( $v, $s, $l );

        # decrement depth level
        $l--;

    }

    # get only unique values
    if( $l == 0 ) $s = array_values( array_unique( $s ) );

    # return stored values
    return $s;

} # end of function array_flat( ...
}

如果这不起作用,那么来自的这个代码段应该会起作用

if( !function_exists( 'array_flat' ) )
{
    function array_flat( $a, $s = array( ), $l = 0 )
{
    # check if this is an array
    if( !is_array( $a ) )                           return $s;

    # go through the array values
    foreach( $a as $k => $v )
    {
        # check if the contained values are arrays
        if( !is_array( $v ) )
        {
            # store the value
            $s[ ]       = $v;

            # move to the next node
            continue;

        }

        # increment depth level
        $l++;

        # replace the content of stored values
        $s              = array_flat( $v, $s, $l );

        # decrement depth level
        $l--;

    }

    # get only unique values
    if( $l == 0 ) $s = array_values( array_unique( $s ) );

    # return stored values
    return $s;

} # end of function array_flat( ...

}

您可以使用array\u unique($array,[,int$sort\u flags]函数。如果未指定可选的sort\u标志,该函数将比较将所有内容转换为字符串的值。如果数组中有字符串以外的值,则可以将sort\u标志指定为以下值之一

SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
来自PHP.net的示例

         $input = array("a" => "green", "red", "b" => "green", "blue", "red");
         $result = array_unique($input);
         print_r($result);
有关更多信息,请参阅

您可以使用array\u unique($array,[,int$sort\u flags]函数。如果未指定可选的sort\u标志,该函数将比较将所有内容转换为字符串的值。如果数组中有字符串以外的值,则可以将sort\u标志指定为以下值之一

SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
来自PHP.net的示例

         $input = array("a" => "green", "red", "b" => "green", "blue", "red");
         $result = array_unique($input);
         print_r($result);
有关更多信息,请参阅

说出示例代码的错误是一个很好的开始……是的,我正在提取重复项,但没有删除它们。我想从原始数组中提取并删除它们。一旦您知道重复项是什么(
$intersected
,循环退出后),然后返回原始数组并删除这些值。我就是这样做的,但什么都没有发生…>foreach($arr)foreach($arr作为$value){if($value==“Orange”){unset($value);}}print_r($arr)}
$value
在该上下文中是数组中任何内容的副本。您需要参考原始数组:
unset($子句[$key1][$key2])
。说出示例代码的错误是一个很好的开始方式……是的,我“我提取重复项,但不删除它们。我想从原始数组中提取并删除它们。一旦您知道重复项是什么(
$intersected
,循环退出后),您将获取该结果并返回原始数组并删除这些值。我这样做,但没有发生任何事情…>foreach($clauses as$arr){foreach($arr as$value){if($value==“Orange”){unset($value);}print_r($arr);}
$value
在该上下文中是数组中任何内容的副本。您需要参考原始数组:
unset($clauses[$key1][$key2])