


What data structure does php have to implement bidirectional index query?
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>
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>
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.
<?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; ?>
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>
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,
$test = [ 'a' => 'a1', 'b' => 'b1', 'c' => 'c1', 'd' => 'd1', 'e' => 'd1' ]; //去除重复的值 $test = array_unique($test);
After removing duplicate values, reverse the array,
$test = [ 'a' => 'a1', 'b' => 'b1', 'c' => 'c1', 'd' => 'd1', 'e' => 'd1' ]; $test = array_unique($test); $wocao = array_flip($test); var_dump($wocao);
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>
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>
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
