-1
$custdata=array();

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

);

This is my array. My persons duplicates, so I can see the same person 1 , 2 ,3 or 5 times in array If i print it. How to remove duplicated values? But I need to do it by DATE. If person exist in array only the newest date leave in array.

elPresta
  • 633
  • 10
  • 17

4 Answers4

0

take a look at array_unique function Since i dont know your exact requirement .This link will help you to understand array_unique function How do I use array_unique on an array of arrays?

Community
  • 1
  • 1
Torrezzzz
  • 307
  • 2
  • 13
0

If I understood your question correctly, what you need to do is simply override the existing person using a unique key (Eg: his email). Here's an example:

<?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.. **/
        );
    }
}
Tom
  • 55
  • 5
0

You need to set an array key for each person on $custdata.

$custdata['person_1'] = array();
$custdata['person_2'] = array();
....

Maybe use their email address as the key.

You can use array_key_exists() to check if a person has already been added. http://php.net/manual/en/function.array-key-exists.php

0

Use the date as the key of the 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 />"
    );

}

In the end, if you don't want to keep the date keys, just use

$custdata = array_values($custdata);

Also, appending <br> to the array values might not be a very good idea.

It might be better to append the <br> when displaying the array.

cornelb
  • 6,046
  • 3
  • 19
  • 30
  • I used email as the key and it works perfect. You are my hero and appreciate your help! Nice, thank u!! – elPresta Aug 06 '14 at 12:31