.PHP.检查数组中是否已经存在一对 [键]=>[值]


PHP. Check if a couple [Key]=>[Value] already exists in an array

我需要检查这样的数组:

(["1"] => "30/11/2001 00:00:00" ["302"] => "2001-12-01 00:00:00" ["522"] =>"01/12/2012 00:00:00" ["1"] => "30/11/2001 00:00:00" etc...)

已经存在我要添加的值对。如果已经存在,我想做一些事情,否则我想做其他事情。

如何在数组(如我写的数组(中找到是否已经存在一对或多对值"["键"] => ["值"] ??

你可以试试:

$tested_key = '302';
$tested_value = '2001-12-01 00:00:00';

if(isset($your_data[$tested_key]) AND $your_data[$tested_key] === $tested_value ){
    echo 'This Key exists with this value';
}

抱歉地告诉你,你的数组上不能有重复的键!

请参阅PHP文档:

http://php.net/manual/en/language.types.array.php#example-97