Jump to content

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
)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.