Home > Backend Development > PHP Tutorial > What data structure does php have to implement bidirectional index query?

What data structure does php have to implement bidirectional index query?

WBOY
Release: 2016-08-10 09:07:16
Original
1262 people have browsed it

I don’t know how to express it. In short, the requirement is somewhat similar to linq in C#.
Suppose there is the following array

<code>$test = [
    'a'=>'a1',
    'b'=>'a1',
    'c'=>'c1',
]
</code>
Copy after login
Copy after login

I can now query 'a1' through $test['a'], but I also want to query the two elements 'a' and 'b' through 'a1'. How can I implement this in php? ?

Supplement: Some people say to use foreach to implement it, but my array may be very large in the later stage, even as large as thousands or tens of thousands. Using foreach is definitely not efficient.

The existing solutions can only solve the situation where the key value of each array element is different, that is, the one-to-one mapping situation. The first one is array_search, and the second one is array_flip first and then the traditional $test['a1'] search.

Reply content:

I don’t know how to express it. In short, the requirement is somewhat similar to linq in C#.
Suppose there is the following array

<code>$test = [
    'a'=>'a1',
    'b'=>'a1',
    'c'=>'c1',
]
</code>
Copy after login
Copy after login

I can now query 'a1' through $test['a'], but I also want to query the two elements 'a' and 'b' through 'a1'. How can I implement this in php? ?

Supplement: Some people say to use foreach to implement it, but my array may be very large in the later stage, even as large as thousands or tens of thousands. Using foreach is definitely not efficient.

The existing solutions can only solve the situation where the key value of each array element is different, that is, the one-to-one mapping situation. The first one is array_search, and the second one is array_flip first and then the traditional $test['a1'] search.

array_search in the manual can solve your needs.

<code class="php"><?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?> </code>
Copy after login

Ask a master in the group to solve the problem, array_keys will be fine.

<code><?php
$a=array("a"=>"Dog","b"=>"Dog","c"=>5,"d"=>"5");
print_r(array_keys($a,"Dog"));
//Array ( [0] => a [1] => b )
 ?>
 </code>
Copy after login

Just enter the code! ~

There is no good way, let’s do it with two arrays

Thank you for the invitation, because the key of the array structure is definitely unique, but different key may correspond to the same value, so as far as I know, if you want to implement c#’s linq, you need to ensure the value of the array The only one,

<code class="php">$test = [
    'a' => 'a1',
    'b' => 'b1',
    'c' => 'c1',
    'd' => 'd1',
    'e' => 'd1'
];

//去除重复的值
$test = array_unique($test);</code>
Copy after login

After removing duplicate values, reverse the array,

<code class="php">$test = [
    'a' => 'a1',
    'b' => 'b1',
    'c' => 'c1',
    'd' => 'd1',
    'e' => 'd1'
];

$test = array_unique($test);

$wocao = array_flip($test);
var_dump($wocao);
</code>
Copy after login

The reverse structure takes the key of the original array as the value, and the value of the original array as the key, as follows

<code>array(4) {
  ["a1"]=> string(1) "a"
  ["b1"]=> string(1) "b"
  ["c1"]=> string(1) "c"
  ["d1"]=> string(1) "d"
}</code>
Copy after login

Now, you can implement linq functions similar to c#

<code>$test = [
    'a' => 'a1',
    'b' => 'b1',
    'c' => 'c1',
    'd' => 'd1',
    'e' => 'd1'
];

$test = array_unique($test);

$wocao = array_flip($test);

$test['a'];   //a1
$wocao['a1']; //a</code>
Copy after login

Please point out any shortcomings,,,

Isn’t it just to query the key based on the value? An array_keys can solve it:

<code>$arr = array('a' => 'a1', 'b' => 'a1', 'c' => 'c1');
var_export( array_keys($arr, 'a1') ); //输出键名 a 和 b</code>
Copy after login
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template