我有一个数组,我想在其中搜索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。

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


当前回答

$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);

其他回答

在PHP的后续版本(>= 5.5.0)中,您可以使用以下一行代码:

$key = array_search('100', array_column($userdb, 'uid'));

我正在寻找类似于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);
    }
function searchForId($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['uid'] === $id) {
           return $key;
       }
   }
   return null;
}

这是可行的。你应该这样称呼它:

$id = searchForId('100', $userdb);

重要的是要知道,如果你使用===运算符比较类型必须完全相同,在这个例子中,你必须搜索字符串或使用==代替===。

根据安哥拉人的回答。在PHP的后续版本(>= 5.5.0)中,可以使用一行程序。

$key = array_search('100', array_column($userdb, 'uid'));

这里是文档:http://php.net/manual/en/function.array-column.php。

我必须使用un函数来查找数组中的每个元素。所以我修改了Jakub truneeek的函数如下:

function search_in_array_r($needle, $array) {
    $found = array();
    foreach ($array as $key => $val) {
        if ($val[1] == $needle) {
            array_push($found, $val[1]);
        }
    }
    if (count($found) != 0)
        return $found;
    else
        return null;
}

如果问题,即。

$a = [
     [
       "_id" => "5a96933414d48831a41901f2",
       "discount_amount" => 3.29,
       "discount_id" => "5a92656a14d488570c2c44a2",
     ],
     [
       "_id" => "5a9790fd14d48879cf16a9e8",
       "discount_amount" => 4.53,
       "discount_id" => "5a9265b914d488548513b122",
     ],
     [
       "_id" => "5a98083614d488191304b6c3",
       "discount_amount" => 15.24,
       "discount_id" => "5a92806a14d48858ff5c2ec3",
     ],
     [
       "_id" => "5a982a4914d48824721eafe3",
       "discount_amount" => 45.74,
       "discount_id" => "5a928ce414d488609e73b443",
     ],
    [
       "_id" => "5a982a4914d48824721eafe55",
       "discount_amount" => 10.26,
       "discount_id" => "5a928ce414d488609e73b443",
     ],
   ];

岁:

function searchForId($id, $array) {
    $did=0;
    $dia=0;
   foreach ($array as $key => $val) {
       if ($val['discount_id'] === $id) {
           $dia +=$val['discount_amount'];
           $did++;
       }
   }
    if($dia != '') {
        echo $dia;
        var_dump($did);
    }
   return null;
};
print_r(searchForId('5a928ce414d488609e73b443',$a));