PHP按值唯一数组? [英] PHP unique array by value?

查看:42
本文介绍了PHP按值唯一数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PHP 中有一个数组,如下所示:

I have an array in PHP that looks like this:

  [0]=>
       array(2) {
           ["name"]=>
              string(9) "My_item"
           ["url"]=>
              string(24) "http://www.my-url.com/"
       }
  [1]=>
     array(2) {
         ["name"]=>
             string(9) "My_item"
         ["url"]=>
            string(24) "http://www.my-url2.com/"
     }

name"中的两个值在这两项中是相同的.我想整理这样的重复项.

The two values in "name" are the same in this two items. I want to sort out duplicates like this.

如何通过检查名称"值来创建唯一数组?

How do I create an unique array by checking the "name" value?

推荐答案

基本上

$unique_array = [];
foreach($your_array as $element) {
    $hash = $element[field-that-should-be-unique];
    $unique_array[$hash] = $element;
}
$result = array_values($unique_array);

这篇关于PHP按值唯一数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆