我有一个数组,我想在其中搜索uid并获得数组的键。

例子

假设我们有以下二维数组:

$userdb = array(
    array(
        'uid' => '100',
        'name' => 'Sandra Shush',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    )
);

函数调用search_by_uid(100)(第一个用户的uid)应该返回0。

函数调用search_by_uid(40489)应该返回2。

我试着做循环,但我想要一个更快的执行代码。


当前回答

这里有一句同样的句子,

$pic_square = $userdb[array_search($uid,array_column($userdb, 'uid'))]['pic_square'];

其他回答

这里有一句同样的句子,

$pic_square = $userdb[array_search($uid,array_column($userdb, 'uid'))]['pic_square'];

这里有一个更好的解决方案,如果您从数据库或多维数组中提取数据

多维数组的例子:

$records = array(
            array(
                'id' => 2135,
                'first_name' => 'John',
                'last_name' => 'Doe',
            ),
            array(
                'id' => 3245,
                'first_name' => 'Sally',
                'last_name' => 'Smith',
            ),
            array(
                'id' => 5342,
                'first_name' => 'Jane',
                'last_name' => 'Jones',
            ),
            array(
                'id' => 5623,
                'first_name' => 'Peter',
                'last_name' => 'Doe',
            )
            );


            function search_user_by_name($name, $array) {
                             foreach ($array as $keys) {
                                 foreach ($keys as $key => $_user_record) {
                                     if ($_user_record == $name) {
                                         return [$key => $_user_record];//Return and array of user
                                     }
                                 }
                             }
                             return null;
                        }

调用函数:

$results = search_user_by_name('John', $records);
         print_r($results);
         

输出:Array ([first_name] => John)

看起来array_filter将是合适的解决方案…

$userdb=Array
(
    (0) => Array
        (
            (uid) => '100',
            (name) => 'Sandra Shush',
            (url) => 'urlof100'
        ),

    (1) => Array
        (
            (uid) => '5465',
            (name) => 'Stefanie Mcmohn',
            (pic_square) => 'urlof100'
        ),

    (2) => Array
        (
            (uid) => '40489',
            (name) => 'Michael',
            (pic_square) => 'urlof40489'
        )
);

PHP代码

<?php 
$search = 5465;
$found = array_filter($userdb,function($v,$k) use ($search){
  return $v['uid'] == $search;
},ARRAY_FILTER_USE_BOTH); // With latest PHP third parameter is optional.. Available Values:- ARRAY_FILTER_USE_BOTH OR ARRAY_FILTER_USE_KEY  

$values= print_r(array_values($found));
$keys =  print_r(array_keys($found)); 
$a = ['x' => ['eee', 'ccc'], 'b' => ['zzz']];

$found = null;
$search = 'eee';

array_walk($a, function ($k, $v) use ($search, &$found) {
    if (in_array($search, $k)) {
        $found = $v;
    }
});

var_dump($found);

我正在寻找类似于MySQL像%term%的功能。根据本页的答案。我能够从文件中搜索JSON数组。

user_list。Json看起来像下面的示例:

{
  "user-23456": {
        "name": "John Doe",
        "age": "20",
        "email": "doe@sample.com",
        "user_id": "23456"
    },
    "user-09876": {
        "name": "Ronojoy Adams",
        "age": "35",
        "email": "joy@sample.com",
        "user_id": "09876"
    },
    "user-34890": {
        "name": "Will Artkin",
        "age": "16",
        "email": "will@sample.com",
        "user_id": "34890"
    },
}

/*
*search_key_like
*/

function search_key_like($value, $key, $array) {
     $results=array();
    $keyword = preg_quote($value, '~');
   foreach ($array as $k => $val) {
//if name a is spell John and keyword is sent as joh or JOH it will return null
//to fix the issue convert the string into lowercase and uppercase
       $data=array($val[$key],strtolower($val[$key]),strtoupper($val[$key]));
       if (preg_grep('~' . $keyword . '~', $data)) {
       array_push($results,$val[$key]);           
    }
   }
   return $results;
}

使用===提取JSON文件===

 $user_list_json='./user_list.json';
    if(file_exists($user_list_json) && file_get_contents($user_list_json)){
    $file_json_data=file_get_contents($user_list_json);
        
    $json_array_data=json_decode($file_json_data,true);
        
        $user_name_like = search_key_like('ron', 'name', $json_array_data);
   
         print "<pre>".print_r($user_name_like,true);
    }