Wednesday, July 31, 2013

Quickly Removing Empty Array Elements in PHP

Different ways of removing empty array slots in PHP.

Removing empty array slots in PHP, leaving holes in the array.

A quick way to remove empty elements from an array is using array_filter without a callback function. This will also remove 0s (zeroes) though.

$myArray = array_filter( $myArray );

Alternatively, array_diff allows you to decide which elements to keep. The following example will only remove empty strings, but keep 0

$myArray = array_diff($myArray, array(''));

Removing empty array slots in PHP, and compacting the array
Both functions leave ‘gaps’ where the empty entries used to be:

$myArray = array( 0, 'red', '', 'blue');

print_r(array_filter($myArray));
Array
(
  [1] => 'red'
  [3] => 'blue'
)

print_r(array_diff($myArray, array('')));
Array
(
  [0] => 0
  [1] => 'red'
  [3] => 'blue'
)

array_slice can remove those gaps:

$myArray = array(0, 'red', '', 'blue');
$myArray = array_filter($myArray);
print_r(array_slice($myArray, 0));
Array
(
  [0] => 'red'
  [1] => 'blue'
)

see whether it is faster than a loop.

One more example

    $data = array(42, "foo", 96, "", 100.96, "php");
   
    // Filter each value of $data through the function is_numeric
    $numeric_data = array_filter($data, "is_numeric");
   
    // Re-key the array so the keys are sequential and numeric (starting at 0)
    $numeric_data = array_values($numeric_data);
   
    // Print out the new, filtered data
    print_r($numeric_data);

The output from above is:

Array
(
    [0] => 42
    [1] => 96
    [2] => 100.96
)

Wednesday, June 19, 2013

Remove duplicate array keys and values with case insensitive.

We have two array.
First array contains some key and values.
Second array contains some keys and values.
I want to merge this two array if duplicate values occurred remove first array duplicate value only.
I want to check key and value is case insensitive.

Please use below code for these requirements.

Code :

<?php
$alname = array(
    "CHE" => "Chennai" ,
    "JKT" => "Jakarta" ,
    "KOL" => "Kolkata" ,
    "MDU" => "Madurai" ,
    "MUM" => "Mumbai" ,
    "SHG" => "Shanghai" );

$alname1 =  array(
    "TKY" => "Tokyo" ,
    "MXI" => "Mexico City" ,
    "DHI" => "Delhi" ,
    "KAR" => "Karachi" ,
    "CHN" => "CheNnai" ,
    "ShG" => "ShaNghai" ,
    "ISB" => "Istanbul" );
   
    echo "<pre>";
    echo "<br/>First Array : <br/>";
    print_r($alname);
    echo "<br/>Second Array : <br/>";
    print_r($alname1);
   
    $arr2 = array_merge($alname,$alname1);
    echo "<br/>Merge first and second array Before remove duplicate values : <br/>";
    print_r($arr2);
   
    foreach($alname as $alkey => $alval)
        {
           $slow_key = strtolower($alkey);
           $slow_val = strtolower($alval);
           $slow_alname1 = unserialize(strtolower(serialize($alname1)));
           $slow_alname1 = array_change_key_case($slow_alname1,CASE_LOWER);
           if(isset($slow_alname1[$slow_key]))
           {
             unset($alname[$alkey]);
           }
          
           if(in_array($slow_val,$slow_alname1))
           {
              unset($alname[$alkey]);
           }
          
        }
        $arr2 = array_merge($alname,$alname1);
        echo "<br/><br/>Merge first and second array After remove duplicate values : <br/><br/>";
        print_r($arr2);
        echo "</pre>";
       
?>


OUTPUT :