-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.

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I used email as the key and it works perfect. You are my hero and appreciate your help! Nice, thank u!!
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?

Comments

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["[email protected]"] = array(
    "date"  => "1/1/1970",
    /** etc.. **/
);

$date = "2/1/1970";
// Override old person with new date
if (isset($person["[email protected]"])) {
    if ($person["[email protected]"]["date"] < $date) {
        $person["[email protected]"] = array(
            "date"  => $date,
            /** etc.. **/
        );
    }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.