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.