Php-mysql Course

In this tutorial you can learn How to Remove, and to get duplicate values from array, and to Reset array keys in PHP.

Remove duplicate values from array

To remove duplicate values from an array, use the PHP function: array_unique(). This function returns a new array removing duplicate values, without changing the key of the remaining elements.
Example:
<?php
// duplicate values: 1, mp
$aray = array(1, 'abc', 1, 'mp', 33, 'mp', 8);
$aray = array_unique($aray);

// test
print_r($aray);      // Array ( [0] => 1 [1] => abc [3] => mp [4] => 33 [6] => 8 )
?>

array_unique() treats the values as string, so, if the array contains for example: 12 (integer), and '12' (string), the function keeps only the first value.
Example:
<?php
// duplicate values: 12, mp
$aray = array(12, 'abc', '12', 'mp', 33, 'mp');
$aray = array_unique($aray);

// test
print_r($aray);      // Array ( [0] => 12 [1] => abc [3] => mp [4] => 33 )
?>

Get duplicate array elements

If you want to get only the duplicate elements from an array, you can use the following construction:
array_unique(array_diff_assoc($aray, array_unique($aray)));
Example:
<?php
// duplicate values: 12, mp
$aray = array(12, 'abc', '12', 'mp', 33, 'mp', 8);
$duplicates = array_unique(array_diff_assoc($aray, array_unique($aray)));

// test
print_r($duplicates);        // Array ( [2] => 12 [5] => mp )
?>

Reset array keys

PHP has not a function especialy to reset array keys, but it can be used another function to get this result.
To reset (or renumber) the keys of an array you can use array_merge(). This function merges the elements of one or more arrays together. In the result array, the numeric keys will be renumbered, starting from zero.
So, if you add only one array with unordered numeric keys, it will return an array with ordered numeric keys, starting from zero, with the values in the same order.
Example:
<?php
$aray = array(1=>'abc', 5=>23, 12=>'mp');
$aray = array_merge($aray);

// test
print_r($aray);      // Array ( [0] => abc [1] => 23 [2] => mp )
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Remove / Get duplicate array values - Reset array keys in PHP

Last accessed pages

  1. The Fifth Agreement (18732)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137634)
  3. Send POST data with file_get_contents (2929)
  4. Egg shape with CSS (3223)
  5. Dynamic variables in JavaScript (18734)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (323)
  2. Read Excel file data in PHP - PhpExcelReader (118)
  3. The Four Agreements (97)
  4. PHP Unzipper - Extract Zip, Rar Archives (94)
  5. The Mastery of Love (86)