Jump to content

Remove values in array2 from array1


johnsmith153

Recommended Posts

I have two arrays.

 

Array 1 is where the array key holds various different numbers. For example:

$array[32] = 1;
$array[122] = 1;
$array[238] = 1;
$array[324] = 1;

 

The other array holds values in the same way (but may have less values in total):

$array[238] = 1;
$array[287] = 1;

 

I want to simply remove any values in array 2 from array 1 (so now array 1 simply holds 32, 122 and 324).

 

I am sure there is a quick array command that does this - so I won't need to use a foreach loop.

Link to comment
https://forums.phpfreaks.com/topic/178762-remove-values-in-array2-from-array1/
Share on other sites

Take a look at the following example and see which of the array functions best suits your needs.

 

<?php

$a[1] = 1;
$a[2] = 1;
$a[3] = 1;
$a[4] = 1;

$b[3] = 1;
$b[4] = 10;

// Diff by key only
print_r(array_diff_key($a, $b));

// Diff by key and value
print_r(array_diff_assoc($a, $b));

?>

 

Array
(
    [1] => 1
    [2] => 1
)
Array
(
    [1] => 1
    [2] => 1
    [4] => 1
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.