Search for a specified key value in the array. Do I have to traverse it? How to solve

Source: Internet
Author: User
Search for a specified key value in the array. Do I have to traverse it? Array format: PHPcodeArray ([0] = & gt; Array ([0] = & gt; ID [1] = & gt; Nickname) [1] = & gt; does Arra need to traverse a specified key value in the array?
The array format is as follows:
PHP code
  Array(    [0] => Array        (            [0] => ID            [1] => Nickname        )    [1] => Array        (            [0] => 29            [1] => Test1        )    [2] => Array        (            [0] => 654            [1] => Xxx1        ))

For example, to find the record with ID = 29, I now use foreach to traverse the array
PHP code
  Foreach ($ array as $ value) {if ($ value [0] = 29) {// The judgment condition is true, perform related operations }}

Is there any more efficient method?

Some people may say that the in_array function is used. However, if the record contains a key value of 29 except for the id, the nickname value may also be 29, which may cause a problem in the query using this function.

------ Solution --------------------
$ Array [1] efficiency
In_array doesn't work. use array_keys ($ array, 29) or array_search (29, $ array)
------ Solution --------------------
PHP code
$array = array(0 => array('id', 'name'), array('29', 'test'), array('65', '29'));array_walk($array, $theValue = 'checkvalue', 29);if ($theValue == true) {    echo 'ok';}function checkvalue($value, $key, $number){    if ($value[0] == $number) {        //something        return true;    }}

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.