多维数组,删除数组,其中键和值匹配另一个数组

时间:2015-07-13 04:18:17

标签: php arrays multidimensional-array

我想删除重复项,其中测量或altunit与另一个数组匹配,但忽略它们是否为空。

Array
(
    [0] => Array
        (
            [id] => 2
            [altunit] => %
            [measurement] => 
        )

    [1] => Array
        (
            [id] => 3
            [altunit] => 
            [measurement] => 6
        )

    [2] => Array
        (
            [id] => 4
            [altunit] => %
            [measurement] => 
        )

    [3] => Array
        (
            [id] => 5
            [altunit] => 
            [measurement] => 6
        )

    [4] => Array
        (
            [id] => 6
            [altunit] => 
            [measurement] => 6
        )

)

变为

Array
(
    [0] => Array
        (
            [id] => 2
            [altunit] => %
            [measurement] => 
        )

    [1] => Array
        (
            [id] => 3
            [altunit] => 
            [measurement] => 6
        )

)

我能想到的最好的是:

$test = array ( 0 => array ( 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ), 1 => array ( 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ), 2 => array ( 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ), 3 => array ( 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ), 4 => array ( 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ), );

$num = [];
foreach($test as $k => $v) $num[] = $v['measurement'];

但这仅适用于测量,并删除id和altunit键。

6 个答案:

答案 0 :(得分:3)

姆, 为测量和altunit创建一个“已知值”数组,然后检查它是否存在于其余值上。

类似的东西:

$knowed_altunit=array();
$knowed_measurement=array();

foreach($test as $k=>$v){
  if(in_array($v['altunit'],$knowed_altunit) 
  || in_array($v['mesurement'],$knowed_measurement)){
    //if the value of altunit or measurement is already knowed then remove the entry from the array,
    unset($test[$k]);
  }else{
   //if it never been seen, add it so further entry can be checked agaisnt the knowed value
   $knowed_altunit[]=$v['altunit'];
   $knowed_measurement[]=$v['mesurement'];
  }
}

很抱歉,如果有任何错字,可能会帮助您解决问题的解决方案。

答案 1 :(得分:2)

您可以尝试以下代码:

<?php

    /* before you need to check that $test variable is declarated and have all items to check */
    $values_altunit = array();
    $values_measurement = array();
    $result = array();

    foreach($test as $key => $value) {
        /* check if exist altunit and measurement values in blacklist arrays */
        if (!in_array($value['altunit'], $values_altunit) && !in_array($value['measurement'], $values_measurement)) {
            /* if not exist, add the item to $result array */
            $result[$key] = $value;
            /* and add altunit and measurement values to blacklist arrays */
            $values_altunit[] = $value['altunit'];
            $values_measurement[] = $value['measurement'];
        }
    }

    /* print result items */
    var_dump($result);

?>

答案 2 :(得分:1)

这是一个简短的代码,可以为独特的测量结果提供结果

  <?php 

    $arr = array(
        "0"=> array        (
                "id" => 2,
                "altunit" => "%",
                "measurement" => "",
            ),

        "1"=> array        (
                "id" => 3,
                "altunit" => "",
                "measurement" => 6,
            ),

        "2"=> array        (
                "id" => 4,
                "altunit" => "%",
                "measurement" => "",
            ),

        "3"=> array        (
                "id" => 5,
                "altunit" => "",
                "measurement" => 6,
            ),

        "4"=> array        (
                "id" => 6,
                "altunit" => "",
                "measurement" => 6,
            )

    );

    $unique_measure = $new_array  = array();
    foreach($arr as $sup_key => $sup_val){
        foreach($sup_val as $sub_key => $sub_val){
            if(!in_array($sup_val['measurement'], $unique_measure)){
                array_push($unique_measure, $sup_val['measurement']);
                array_push($new_array,$sup_val);

            }

        }
    }

    print_r($new_array);
?>
  

输出:

  Array
    (
        [0] => Array
            (
                [id] => 2
                [altunit] => %
                [measurement] => 
            )

        [1] => Array
            (
                [id] => 3
                [altunit] => 
                [measurement] => 6
            )

    )   

试试这段代码。它可以做到这一点。

您的问题解决方案的另一种方法是使用unset(your_array_key)从主阵列中删除该特定键,将在相同的代码中执行此操作。

答案 3 :(得分:1)

试试,这可能会对你有所帮助

function remove_dup($array, $keys )
{
    $out = array();

    foreach($array as $sub)
    {
        if(empty($out))
        {   
            $out[] = $sub;
            continue;
        }

        foreach($keys as $key)
        {   
            if($flag=in_array( $sub[$key],array_map(function($e) use($key){ return $e[$key];}, $out)) )
                break;
        }

        if(!$flag)
            $out[] = $sub;
    }

    return $out;
}

// Usage
print_r( remove_dup($array, array('altunit','measurement') ) );

<强>测试

[akshay@localhost tmp]$ cat test.php 
<?php

function remove_dup($array, $keys )
{
    $out = array();

    foreach($array as $sub)
    {
        if(empty($out))
        {   
            $out[] = $sub;
            continue;
        }

        foreach($keys as $key)
        {   
            if($flag=in_array( $sub[$key],array_map(function($e) use($key){ return $e[$key];}, $out)) )
                break;
        }

        if(!$flag)
            $out[] = $sub;
    }

    return $out;
}

$array = array(
        "0"=> array      (
                "id" => 2,
                "altunit" => "%",
                "measurement" => "",
            ),

        "1"=> array        (
                "id" => 3,
                "altunit" => "",
                "measurement" => 6,
            ),

        "2"=> array        (
                "id" => 4,
                "altunit" => "%",
                "measurement" => "",
            ),

        "3"=> array        (
                "id" => 5,
                "altunit" => "",
                "measurement" => 4,
            ),

        "4"=> array        (
                "id" => 6,
                "altunit" => "",
                "measurement" => 6,
            )

    );

print_r( remove_dup($array, array('altunit','measurement') ) );


?>

<强>输出

[akshay@localhost tmp]$ php test.php 
Array
(
    [0] => Array
        (
            [id] => 2
            [altunit] => %
            [measurement] => 
        )

    [1] => Array
        (
            [id] => 3
            [altunit] => 
            [measurement] => 6
        )

)

答案 4 :(得分:0)

你真正想要的是设置,而不是数组。因此,如果您无法解决首先构建阵列的方式(我猜这是来自SQL查询,这将是更少的代码来纠正)你有两个用于在PHP中创建映射集的选项。

  1. 您可以使用SplObjectStorage
  2. 您可以使用Array将密钥作为
  3. 序列化表示形式

    第一种方法看起来像这样......

    $set = new SplObjectStorage();
    
    $arr = [
        0 => [
            'id'          => '2',
            'altunit'     => '%',
            'measurement' => NULL,
        ],
        1 => [
            'id'          => '3',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
        2 => [
            'id'          => '4',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
        3 => [
            'id'          => '5',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
        4 => [
            'id'          => '6',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
    ];
    
    foreach($arr as $part) {
        if (isset($part['altunit'])) { // ignore if blank
            $key = (object) $part;
            $set->attach($key); // attach the set
        }
    }
    
    // Now you have...
    foreach($set as $value) {
        var_dump($value);
    }
    

    这会给你......

    object(stdClass)#2 (3) {
      ["id"]=>
      string(1) "2"
      ["altunit"]=>
      string(1) "%"
      ["measurement"]=>
      NULL
    }
    

    使用数组的第二种方法,其中数组键表示集合的序列化版本,看起来像这样......

    $set = [];
    
    $arr = [
        0 => [
            'id'          => '2',
            'altunit'     => '%',
            'measurement' => NULL,
        ],
        1 => [
            'id'          => '3',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
        2 => [
            'id'          => '4',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
        3 => [
            'id'          => '5',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
        4 => [
            'id'          => '6',
            'altunit'     => NULL,
            'measurement' => '6',
        ],
    ];
    
    foreach($arr as $part) {
        if (isset($part['altunit'])) { // ignore if blank
            $key = "{$part['altunit']}\0{$part['measurement']}";
            $set[$key] = $part; // attach the set
        }
    }
    
    // Now you have...
    foreach($set as $value) {
        var_dump($value);
    }
    

    这会给你......

    array(3) {
      ["id"]=>
      string(1) "2"
      ["altunit"]=>
      string(1) "%"
      ["measurement"]=>
      NULL
    }
    

    N.B

    但是,如果这是SQL查询的结果集,则很可能通过修改查询以使用WHERE NOT NULLGROUP BY子句来完全消除重复数据删除过程。

答案 5 :(得分:0)

You can try this also
 <?php
      $keys    = array_map(function ($i) { return $i; }, $array);
      $dumparr = array_combine($keys, $array);
      $result  = array_values($deduped);
      print_r($result);
?>