81446

How to remove duplicate values in PHP? [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/307650/how-to-remove-duplicate-values-from-an-array-in-php" dir="ltr" rel="nofollow">How to remove duplicate values from an array in PHP</a> <span class="question-originals-answer-count"> 21 answers </span> </li> </ul>

I need help. Want to remove duplicate values from array in php.

$categoryTotal echoes 144 125 262 108 351 177 266 269 270 268 309 144 125 262 238 108

On using:

vardump()

I get

Array ( [0] => 144 [1] => 125 [2] => 262 [3] => 108 [4] => 351 [5] => 177 [6] => 266 [7] => 269 [8] => 270 [9] => 268 [10] => 309 [14] => 238 )

I used sort to sort the values in ascending order I get:

108 108 125 125 144 144 177 238 262 262 266 268 269 270 309 351

but then using $categoryTotal=array_unique($categoryTotal,SORT_NUMERIC); few values like 309 351 disappear.

Would like to know how to know out duplicate values from this array.

Answer1:

Asked before.

$array = array(1, 2, 2, 3); $array = array_unique($array); // Array is now (1, 2, 3)

Answer2:

Use the array_unique() function, like this:

$noDuplicates = array_unique($categoryTotal);

<a href="http://php.net/manual/en/function.array-unique.php" rel="nofollow">Docs here</a>.

Recommend

  • Retrieve values configuration settings MediaWiki
  • php filter var returning a wrong result
  • PHP Not Echoing in the right place
  • Looping through a table with Simple HTML DOM
  • PowerShell understand Get-Member
  • how to set variables in a php include file?
  • How to assign byte[] as a pointer in C#
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Appending Character to Character Array In C
  • PHP CURL timing out but CLI CURL works
  • Declaring variable dynamically in VB.net
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Jackson Parser: ignore deserializing for type mismatch
  • How to define and use opencv mat of user type
  • Refering to the class itself from within a class mehod in Objective C
  • Extracting HTML between tags
  • Get data from AJAX - How to
  • ImageMagick, replace semi-transparent white with opaque white
  • Ajax Loaded meta Tags
  • Initializer list vs. initialization method
  • Xamarin Forms - UWP Fonts
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Regex thinks I'm nesting, but I'm not
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • What is the “return” in scheme?
  • Date difference with leap year
  • vba code to select only visible cells in specific column except heading
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Arrays break string types in Julia
  • How to disable jQuery.jplayer autoplay?
  • A cron job substitute?
  • json Serialization in asp
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • IndexOutOfRangeException on multidimensional array despite using GetLength check