An algorithm solution

Source: Internet
Author: User
An algorithm has two-dimensional arrays: PHPcode $ a = array ('id' = & gt; '1', 'name' = & gt; 'A ', 'URL' = & gt; 'C'), array ('id' = & gt; '2', 'name' = & gt; 'a
There are two-dimensional arrays
PHP code
  $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'));

How can we get array ('id' => '3', 'name' => 'AAA', 'URL' => 'CCC? This means to compare the items except id in two arrays. if the value in array a does not exist in array B, filter the items and do not want to use nested foreach, every array has tens of thousands of records, so efficiency is a problem. I would like to ask you for advice.

------ Solution --------------------
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 ($ );
------ Solution --------------------
Discussion

Algorithm time
If (! $ T [$ v ['name']. $ v ['URL']) is the key to query an array. it is also a query list with the complexity of n.

------ Solution --------------------
Isset determines whether a value is in the list
It doesn't matter whether it's a key or a value, or how your data is organized.
In short, he queries the list. the worst case is to retrieve the last element of the list.

In fact, isset and in_array are similar.

Someone raised a question in a similar issue of mine (using in_array ).
It is very simple. for algorithms alone, it cannot be included because php is implemented internally.

The performance of hash tables is better than that of linear tables, but the time complexity is the same.
Of course, it does not rule out that my high school education has influenced my understanding of theoretical knowledge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.