Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php按键的唯一数组 $custdata=array(); $custdata[]=数组( “firstname”=>$row['firstname']。“”, “lastname”=>$row['lastname']。“”, “email”=>$row['email']。“”, “日期”=>$row['date']。” );_Php_Arrays_Unique - Fatal编程技术网

php按键的唯一数组 $custdata=array(); $custdata[]=数组( “firstname”=>$row['firstname']。“”, “lastname”=>$row['lastname']。“”, “email”=>$row['email']。“”, “日期”=>$row['date']。” );

php按键的唯一数组 $custdata=array(); $custdata[]=数组( “firstname”=>$row['firstname']。“”, “lastname”=>$row['lastname']。“”, “email”=>$row['email']。“”, “日期”=>$row['date']。” );,php,arrays,unique,Php,Arrays,Unique,这是我的阵列。我的人是重复的,所以如果我打印它,我可以在数组中看到同一个人1、2、3或5次。如何删除重复的值?但我必须在日期前完成。如果数组中存在人员,则只保留最新的日期。请查看数组的独特功能,因为我不知道您的确切要求。此链接将帮助您了解数组的独特功能 如果我正确理解了您的问题,您需要做的就是使用唯一的密钥(例如:他的电子邮件)覆盖现有的人员。下面是一个例子: $custdata=array(); $custdata[] = array( "firstname" => $row[

这是我的阵列。我的人是重复的,所以如果我打印它,我可以在数组中看到同一个人1、2、3或5次。如何删除重复的值?但我必须在日期前完成。如果数组中存在人员,则只保留最新的日期。

请查看
数组的独特功能,因为我不知道您的确切要求。此链接将帮助您了解数组的独特功能

如果我正确理解了您的问题,您需要做的就是使用唯一的密钥(例如:他的电子邮件)覆盖现有的人员。下面是一个例子:

$custdata=array();

$custdata[] = array(
    "firstname" => $row['firstname'] ."<br /> <br />",
    "lastname" => $row['lastname'] ."<br /> <br />",
    "email" => $row['email'] ."<br />",
     "date" => $row['date'] ."<br />"

);

您需要为$custdata上的每个人设置一个数组键

<?php

$persons = array();

$persons["123@gmail.com"] = array(
    "date"  => "1/1/1970",
    /** etc.. **/
);

$date = "2/1/1970";
// Override old person with new date
if (isset($person["123@gmail.com"])) {
    if ($person["123@gmail.com"]["date"] < $date) {
        $person["123@gmail.com"] = array(
            "date"  => $date,
            /** etc.. **/
        );
    }
}
也许用他们的电子邮件地址作为钥匙


您可以使用array_key_exists()检查是否已经添加了一个人

使用日期作为数组的键

$custdata['person_1'] = array();
$custdata['person_2'] = array();
....
此外,将

附加到数组值可能不是一个好主意


在显示数组时,最好附加

我使用电子邮件作为键,它工作得非常好。你是我的英雄,感谢你的帮助!很好,谢谢你!!
$custdata = array();

// a loop probably goes here    

if (empty($custdata[$row['date']])) {
    $custdata[$row['date']] = array(
        "firstname" => $row['firstname'] ."<br /> <br />",
        "lastname"  => $row['lastname'] ."<br /> <br />",
        "email"     => $row['email'] ."<br />",
        "date"      => $row['date'] ."<br />"
    );

}
$custdata = array_values($custdata);