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 HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
<nav><ul>
 <li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
 <li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
}
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
Remove / Get duplicate array values - Reset array keys in PHP

Last accessed pages

  1. Get the Timestamp of First and Last day of Month (1122)
  2. Convert BBCode to HTML and HTML to BBCode with JavaScript (9272)
  3. Using v-model in form input fields (843)
  4. Rotate HTML objects, Div, Span, images with jQuery (7987)
  5. Add and Remove HTML elements and Content with jQuery (30938)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (199)
  2. Read Excel file data in PHP - PhpExcelReader (72)
  3. The Four Agreements (65)
  4. PHP Unzipper - Extract Zip, Rar Archives (63)
  5. Get and Modify content of an Iframe (49)
Chat
Chat or leave a message for the other users
Full screenInchide