Home Backend Development PHP Tutorial 求高手一个算法解决办法

求高手一个算法解决办法

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'=&gt;'1','name'=&gt;'a','url'=&gt;'c'),    array('id'=&gt;'2','name'=&gt;'aa','url'=&gt;'cc'),        array('id'=&gt;'3','name'=&gt;'aaa','url'=&gt;'ccc'));$b=array(    array('name'=&gt;'aa','url'=&gt;'cc'),    array('name'=&gt;'a','url'=&gt;'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=&gt;$v)     if(!$t[$v['name'].$v['url']]) unset($a[$k]);     print_r($a);<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

从算法时间度上考量
if(!$t[$v['name'].$v['url']]) 是查询数组的键,同样也是查询列表,复杂度为 n

------解决方案--------------------
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 尊渡假赌尊渡假赌尊渡假赌

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 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

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? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

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 How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

How to determine the number of foreach loop in php

PHP function introduction—get_headers(): Get the response header information of the URL PHP function introduction—get_headers(): Get the response header information of the URL Jul 25, 2023 am 09:05 AM

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 Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

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 get your Steam ID in a few steps? May 08, 2023 pm 11:43 PM

How to get your Steam ID in a few steps?

How to use URL encoding and decoding in Java How to use URL encoding and decoding in Java May 08, 2023 pm 05:46 PM

How to use URL encoding and decoding in Java

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

See all articles