Remove Duplicate String Array Elements Using PHP

PHP logo transparent background

Just using for each loop, you can populate a new array, check if there's already an element with the same values, if is exists than skip that row. Look at this codes below for removing duplicate string array elements in PHP.

<?php

function filter_duplicate($ori){
  $result = array();
  $labels= array();
  $i=0;
  foreach ($ori as $row) {
    $label = $row['label'];
    if( !in_array($label, $labels) ){
      $result[$i] = $row;
      $labels[$i] = $label;
      $i++;
    }
  }
  return $result;
}

$original_array=array(
  array(
    "name"=>"name1",
    "label"=>"name dup 1",
  ),
  array(
    "name"=>"name2",
    "label"=>"name dup 1",
  ),
  array(
    "name"=>"name3",
    "label"=>"name dup 3",
  ),
  array(
    "name"=>"name4",
    "label"=>"name dup 3",
  )
);

$no_duplicate = filter_duplicate($original_array);
var_dump($no_duplicate);


That example code above is only filtering one field 'label', it prevents only label from duplication. You can also also filter the field name to remove duplicate values, just change the $label = $row['label'] to $label = $row['name']. In_array function to check the if any value inside the array.

You can use array_unique if you want both name and label field can not have the same value. So every row of your array is unique. Just do $no_duplicate=filter_duplicate($original_array). But array unique is only used for not multidimensional array (array with more than two dimensions).

Dealing with multi dimensional array like as you can see in the example above i provide, the trick to manipulate arrays, all you need to do is just create new temporary array to store the array result you want, then loop through the original array, then within the loop, do some of your codes logic to populate the temporary array with array value whatever you want to filter.

That's in logic is not that hard, and performance because it is require looping through all the list, it may cause performance issue if the list we provided is really big, so you need to do some paging if you want to create list, never store infinite list to your array. Even maybe the core PHP developer already did some performance on the underlying PHP core.

I think PHP already done several advancement on dealing with huge number of elements inside an array on the recent version of PHP, in PHP version 7 and up. As long as your not calling IO or database queries inside that for each loop, if will not cause that much big performance issue. Also in the PHP.ini, never set to much max_execution_time, just leave that alone with its default value. You just need to do some performance optimization on your codes, not changing any PHP default configuration that will only leads into security issues.

Popular posts from this blog

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously

How To Connect SSH Using PEM Certificate On Windows

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Upload and Download Rest API using Spring Boot Reactive WebFlux