本文介绍了从数组中删除重复项(阵列通过按键UNIC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 阵列

    [0] =>排列
        (
            [文件] => /var/websites/example.com/assets/images/200px/1419050406e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
            [MD5] => 42479bee7a304d2318250de2ef1962a9
            [URL] => http://example.com/assets/images/200px/1419050406e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
        )    [1] =>排列
        (
            [文件] => /var/websites/example.com/assets/images/200px/21277792606e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
            [MD5] => 42479bee7a304d2318250de2ef1962a9
            [URL] => http://example.com/assets/images/200px/21277792606e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
        )

我怎样才能从上面数组中删除MD5密钥副本?


解决方案

 < PHP
$数据=阵列(
  阵列(
    'MD5'=> 'α',
    某些=> '值',
  )
  阵列(
    'MD5'=> 'α',
    某些=> 其他的价值,
  )
  阵列(
    'MD5'=> 喝彩,
    某些=> 第三值',
  )
);
//行走输入数组
$ _data =阵列();
的foreach($的数据作为V $){
  如果(使用isset($ _数据[$ V ['MD5'])){
    //发现重复
    继续;
  }
  //记住的特定项目
  $ _data [$ V ['MD5'] = $ V;
}
//如果您需要$ _data零基于阵列,otheriwse工作
$数据= array_values​​($ _数据);
Array
(
    [0] => Array
        (
            [file] => /var/websites/example.com/assets/images/200px/1419050406e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
            [md5] => 42479bee7a304d2318250de2ef1962a9
            [url] => http://example.com/assets/images/200px/1419050406e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
        )

    [1] => Array
        (
            [file] => /var/websites/example.com/assets/images/200px/21277792606e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
            [md5] => 42479bee7a304d2318250de2ef1962a9
            [url] => http://example.com/assets/images/200px/21277792606e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
        )
)

How can I remove md5 key duplicates from above array?

解决方案
<?php
$data = array(
  array(
    'md5' => 'alpha',
    'some' => 'value',
  ),
  array(
    'md5' => 'alpha',
    'some' => 'other value',
  ),
  array(
    'md5' => 'bravo',
    'some' => 'third value',
  ),
);
// walk input array
$_data = array();
foreach ($data as $v) {
  if (isset($_data[$v['md5']])) {
    // found duplicate
    continue;
  }
  // remember unique item
  $_data[$v['md5']] = $v;
}
// if you need a zero-based array, otheriwse work with $_data
$data = array_values($_data);

这篇关于从数组中删除重复项(阵列通过按键UNIC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 03:28