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; }}