elements

How to emove duplicate values from PHP Array? explain with example

PHP array_unique

PHP array_unique() function to remove duplicate elements or values from an array. If the array contains the string keys, then this function will keep the first key encountered for every value, and ignore all the subsequent keys.

Example:-

<?php
   $array = array("a" => "One", "Tow", "One" => "Tow", "Three", "Four");
 
   // Deleting the duplicate items
   $result = array_unique($array);
   print_r($result);
?> 

Output:-

Removing duplicate values from an array in PHP

To remove duplicate values from an array in PHP, use the array_unique() function. If two or more array values are the same, the first appearance will be kept, and the other will be removed. The returned array will hold the first array item’s key type.

Syntax:-

array_unique(array, [sortingType])

Arguments

The array parameter is required, which specifies the array.

sortingType parameter is optional, and it specifies how to compare the array element. The following are some soringType flags.

  1. SORT_REGULAR – compare items usually (don’t change types)
  2. SORT_NUMERIC – compare items numerically
  3. SORT_STRING – compare items as strings
  4. SORT_LOCAL_STRING – compare items as strings based on the current locale.

Example:-

<?php

   $data = [20, 21, 21, 22, 21, 23, 22, 46, 47];

   print_r(array_unique($data));

?> 

Output:-

Associative Array in PHP array_unique() Function

Let’s pass the associative array to the array_unique function and see the output.

Example:-

<?php

    $data = ['a' => 'Vijay', 
        'b' => 'Amit',
        'd' => 'Vijay', 
        'f' => 'Amit', 
        'c' => 'Rahul', 
        'k' => 'Roshan', 
        'n' => 'Vijay'];
    $output = array_unique($data);
    print_r($output);

?> 

Output:-

Create a multidimensional array unique

You can create a multidimensional array unique for any single key index. 

Example:-

<?php

    function unique_multi_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 
    
    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
  }
  
  $data = array( 
    0 => array("id"=>"1", "name"=>"Vijay",  "age"=>"26"), 
    1 => array("id"=>"2", "name"=>"Amit", "age"=>"25"), 
    2 => array("id"=>"1", "name"=>"Rahul",  "age"=>"26"), 
  ); 
  $output = unique_multi_array($data,'id');
  print_r($output);

?> 

Output:-

What is the difference between count or sizeof function in PHP? explain with example

According to PHP official documentation, there is no difference between count() or sizeof() function in PHP, which means both are the same, the sizeof() function is just the alias of count function i.e. the sizeof() uses the same as count function underneath. Both are used to count elements in an array.sizeof() function is an alias of count() function used in PHP. count() function is faster and butter than sizeof().

count() method: 

The count() method is used to calculate all the elements in the array or any other countable object. It can be used for both uni-dimensional as well as multi-dimensional arrays. 

count(array)

Example:-


<?php
        $parts = array(

                "Ram" => array(
                "SSD",
                "Processor"
            ),
                "Key" => array(
                "Monitor"  
            ),
                "Mouse" => array(
                "Cebal"
            )
        );
        
        print_r($parts);
        print("<br>");
        
        echo "Sub elements of an array: "
            . count($parts) . "<br>";
        echo "All elements of an array: "
            . count($parts, 1);
        
?>

Output:-

sizeof() method: 

The sizeof() method is used to calculate all the elements present in an array or any other countable object. It can be used for both uni-dimensional as well as multi-dimensional arrays. 

sizeof(arr, mode)

Example:-

<?php
    $parts = array(

        "Computer" => array(
        "SSD",
        "Processor"
    ),
        "Keyboard" => array(
        "Monitor"  
    ),
        "Mouse" => array(
        "CPU"
    )
);
    
    print_r($parts);
    print("<br>");
    
    echo "Sub elements of an array: "
        . sizeof($parts) . "<br>";
    echo "All elements of an array: "
        . sizeof($parts, 1);
 
?>

Output:-

Difference between sizeof() and count() methods:

  • The sizeof() method takes a longer execution time.
  • The sizeof() method is an alias of the count() method.
count() sizeof()
1. The count() returns the number of elements in the array.The sizeof() function is used to return the number of elements in an array.
2. Its syntax is -:
count(array, mode)
Its syntax is -:
sizeof(array, mode)
3. Its return value is of integer type.Its return value is of integer type.
4. The count() function may return 0 for a variable that is not set.This function is an alias of count() function.
5. It is supported in PHP version 4.0+It is supported in PHP version 4.0+

How to get number of elements in an Array? explain with example

PHP offers count() and sizeof() functions. The count() and sizeof() both functions are used to count all elements in an array and return 0 for a variable that has been initialized with an empty array. These are the built-in functions of PHP. We can use either count() or sizeof() function to count the total number of elements present in an array.

Syntax:

count(array)

Example:-

<?php
  $days = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 

  echo 'Total number of elements in the $days array is - ' . count($days);
  echo "<br>";
  echo 'Total number of elements in the $days array is - ' . sizeof($days);
?>  

Output:-

Total number of elements in the $days array is - 7
Total number of elements in the $days array is - 7