Showing posts with label PHP array_diff. Show all posts
Showing posts with label PHP array_diff. Show all posts

Friday, 2 August 2019

How to remove values in one array from another array using PHP

Example: I have array A with values and another array B. I need to remove the values in array A from the values in array B.

  1. Use the PHP function array_diff
    • Description
      • array array_diff ( array $array1 , array $array2 [, array $ … ] )
      • Compares array1  against array2  and returns the difference.
    • Parameters
      • array1 = The array to compare from
      • array2 = An array to compare against
  2. Code example below:
    <?php
    
        $arrayA = array('one','two');
    
        $arrayB = array('one','two','three','four','five');
    
        
    
        $newArray = array_diff($arrayB,$arrayA);
    
        
    
        echo '<pre>';
    
        print_r($newArray);
    
        echo '</pre>';
    
    ?>
  3. The code example above will print the following:
    Array
    
    (
    
        [2] => three
    
        [3] => four
    
        [4] => five
    
    )
  4. Explanation:
    • Array A values will be removed from array B
    • The $newArray variable will contain the values of array B with values from array A removed

Thursday, 30 August 2018

PHP: Sort set based on another table


 I have visited similar question here but not getting what i want in php. Suppose i have 2 arrays. All checking should be case insensitive. say Field0 is same as field0 or fiEld1 is same as Field1.


array1 is array(
"0"=>"field0",
"1"=>"field1",
"2"=>"field2",
"3"=>"field3",
);

array2 is array(
"0"=>"field3",
"1"=>"field2",
"2"=>"field0",
"3"=>"field1",
"4"=>"field6",
"5"=>"field5",
);

Now I want array2 to be sorted based on array1 like the following:
array2 is array(
"0"=>"field0",
"1"=>"field1",
"2"=>"field2",
"3"=>"field3",
"4"=>"field6",
"5"=>"field5",
);

look here array1 has 4 elements so array2 should be sorted based exactly 4 elements of array1 and rest 2 element (index 4,5 of array2) should as it is in array2 before.

Try array_intersect combined with array_diff:
<?php
$array1 = array(
    "0"=>"field0",
    "1"=>"field1",
    "2"=>"field2",
    "3"=>"field3",
);

$array2 = array(
    "0"=>"field3",
    "1"=>"field2",
    "2"=>"field0",
    "3"=>"field1",
    "4"=>"field6",
    "5"=>"field5",
);

$array3 = array_merge(
    array_intersect($array1, $array2),
    array_diff($array2, $array1)
);

var_dump($array3);

Update
For a case insensitive approach, use array_map to guarantee all entries in both arrays are lower-cased:
$array3 = array_map('strtolower', $array1);
$array4 = array_map('strtolower', $array2);

$array5 = array_merge(
    array_intersect($array3, $array4),
    array_diff($array4, $array3)
);

var_dump($array5);

Wednesday, 24 September 2014

array_diff in PHP

PHP array_diff() function returns an array consisting only of those values present in the first input array and not present in any other arrays.

Syntax:  array_diff(array1,array2,array3...)

Parameters Description:
array1 : Required. The first array will be compared with other arrays.
array2 : Required. An array to be compared with the first array.
array3 : Optional. An array to be compared with the first array.

Tip : The first array can compare with single array or more arrays.
Note : Compare only values of the array.

Example:


<?php
$daysarr1 = array(0=>"Sunday",1=>"Monday",2=>"Tuesday",3=>"Wednesday");
$daysarr2 = array(4=>"Monday",5=>"Tuesday",6=>"Friday");
print_r( array_diff($daysarr1,$daysarr2) );
?>

Output:


Array (
          [0] => Sunday
          [3] => Wednesday
       )