Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 5

PHP : How to Find Duplicates, Remove Them, Preserve Last Value in Multidimensional Array?

I have multidimensional array in PHP like this :

1, A

2, B

3, C

4, A

5, D

6, A

I want it processed become :

2, B

3, C

5, D

6, A

i've search over internet but they preserve the first value not the last value

6 Answers

Relevance
  • 8 years ago
    Favourite answer

    That doesn't look like a multidimensional Array to me. If you don't have several Alphabetical values assigned to the same number, then its not multidimensional just pairs of values.

    To remove duplicate amounts from an array, use this function:

    http://php.net/manual/en/function.array-unique.php

    If you don't want the function to preserve first but last duplicates, you must trick it.

    Load the array backwards, run the function, output the array backwards again. That way it will seem it Preserved the last duplicate when it actually just worked with a reversed array.

    You can reverse it using this function:

    http://php.net/manual/en/function.array-reverse.ph...

    Good luck.

  • Anonymous
    6 years ago

    This Site Might Help You.

    RE:

    PHP : How to Find Duplicates, Remove Them, Preserve Last Value in Multidimensional Array?

    I have multidimensional array in PHP like this :

    1, A

    2, B

    3, C

    4, A

    5, D

    6, A

    I want it processed become :

    2, B

    3, C

    5, D

    6, A

    i've search over internet but they preserve the first value not the last value

    Source(s): php find duplicates remove preserve multidimensional array: https://tr.im/H1u5q
  • ?
    Lv 4
    4 years ago

    Php Array Find

  • ?
    Lv 4
    5 years ago

    Php Find In Array

  • Anonymous
    5 years ago

    I have never programmed in C before but from my knowledge of general programming I would suggest sorting the array alphabetically then stepping through each value of the array and comparing it to a stored variable of the previous value. If they are different; put the value in a second array. If they are the same move on to the next value. Sorry if that isn't useful in C.

  • Anonymous
    8 years ago

    <?php

    $arr = array("A","B","C","A","D","A");

    $count = 0;

    $temp = array();

    for($i=0;$i<6;$i++)

    {

    for($j=$i+1;$j<6;$j++)

    {

    if($arr[$i]==$arr[$j])

    {

    $count++;

    $arr[$j] = $arr[$j+1];

    }

    }

    }

    echo "After Removing the Duplicates, the array is: ";

    foreach($arr as $x)

    {

    echo $x;

    }

    ?>

    I hope this code helps you some way.

Still have questions? Get answers by asking now.