求高手一个算法解决办法
Jun 13, 2016 am 10:14 AM
array
foreach
gt
name
url
求高手一个算法
有两个二维数组
- PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$a=array( array('id'=>'1','name'=>'a','url'=>'c'), array('id'=>'2','name'=>'aa','url'=>'cc'), array('id'=>'3','name'=>'aaa','url'=>'ccc'));$b=array( array('name'=>'aa','url'=>'cc'), array('name'=>'a','url'=>'c'));
Copy after login
如何能够得到array('id'=>'3','name'=>'aaa','url'=>'ccc')呢?意思就是说,比较两个数组中除了id的项,如果数组a中的值在数组b中不存在,则筛选出来,不希望使用嵌套foreach,每个数组中都有好几万条记录,所以效率是个问题。请教各位,还望有思路的朋友赐教。
------解决方案--------------------
- PHP code
$t=array();foreach($b as $vl) $t[$vl['name'].$vl['url']]=$vl;foreach($a as $k=>$v) if(!$t[$v['name'].$v['url']]) unset($a[$k]); print_r($a);<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
------解决方案--------------------
isset 判断一个值是否在列表中
并无所谓是键还是值,也无所谓你的数据是如何组织的
总之,他是在列表中查询,最坏的情况就是检索到列表的最后一个元素
其实 isset 与 in_array 是相似的
就有人在我的某个类似问题中(使用了 in_array)提出了质疑
很简单,仅就算法而言,并不能因为是php内部实现的,就不计算在内
哈希表的性能优于线性表,但在时间复杂度上还是一样的
当然并不排除我的高中学历影响了对理论知识的理解
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot tools Tags

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the differences between Huawei GT3 Pro and GT4?

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList?

How to determine the number of foreach loop in php

PHP function introduction—get_headers(): Get the response header information of the URL

Why NameResolutionError(self.host, self, e) from e and how to solve it

How to get your Steam ID in a few steps?

How to use URL encoding and decoding in Java

Fix: Snipping tool not working in Windows 11
