从PHP中的关联数组数组中删除重复/排序 [英] Remove dupes/sort from a Array of Associative Arrays in PHP

查看:198
本文介绍了从PHP中的关联数组数组中删除重复/排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关联数组数组


aa[] = ('Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 );
aa[] = ('Tires'=>454, 'Oil'=>43, 'Spark Plugs'=>3 );
aa[] = ('Tires'=>34,  'Oil'=>55, 'Spark Plugs'=>44 );
aa[] = ('Tires'=>454, 'Oil'=>43, 'Spark Plugs'=>45 );
aa[] = ('Tires'=>34,  'Oil'=>55, 'Spark Plugs'=>433 );
aa[] = ('Tires'=>23,  'Oil'=>33, 'Spark Plugs'=>44 );

两个问题


  1. 我可以删除重复的字段'油'
    有一个array_unique我可以提供一个回调作为一个自定义的比较器?

  1. How can I remove duplicates according tot he field 'Oil' is there a array_unique which I can provide a callback which acts as a custom comparator?

如何按照自定义字段Spark Plugs排序

How can I sort by a custom field 'Spark Plugs'


推荐答案

而不是手动去做一般的重复检查,我做了这个

Instead of manually going and doing the usual duplicate checking, I did this




$aa2 = array()

foeach($aa as $key => $value)  {
  $aa2[$value['Oil']] = $value;
}
$aa = $aa2;

然后按键完成排序...

Then sorting was done by the key...

这篇关于从PHP中的关联数组数组中删除重复/排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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