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 08:48:52
Original
997 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

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.

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