Array_diff() removes too many elements

Asked
Active3 hr before
Viewed126 times

6 Answers

removesarrayelements
90%

HI i have used array_filter to filter the values of $SplitInventory1 array using $SplitInvoice. If array_filter's callback function return true then present value will be placed into new array.,I use the code below to get the difference from two arrays. But as it seems right now it removes too many elements or I'm doing something wrong., 1 For All others as SO is a learning site as all did everyone google for new knowledge. Patient is the key of success. I am also a new learner if i am answering any question by copying or searching from anywhere its my thought that i will learn something new. i have not forced anyone to upvote this or accept this – Mamta Aug 11 '16 at 12:56 , "array_diff returns the values in array1 that are not present in any of the other arrays" – 111 exists in the other array, why should it be returned? – deceze ♦ Aug 11 '16 at 13:06

3) One more thing I have unset $SplitInvoice array if a value found beacuse we need dublicate entries( due to 111 if $SplitInvoice is not unset then it will not come in $diff )

<?php
    $Inventory1 = "111,222,333,111";
    $SplitInventory1 = explode(",",$Inventory1);
    $Invoice = "111,222";
    $SplitInvoice = explode(",",$Invoice);
    $diff = array_filter($SplitInventory1, 
  function ($val) use (&$SplitInvoice) { 
    $key = array_search($val,$SplitInvoice);
    if ( $key === false ) return true;
    unset($SplitInvoice[$key]);
    return false;
  }
);

echo "<pre>";print_r($diff);

?>
load more v
88%

Example #2 array_diff() example with non-matching types,Example #1 array_diff() example,array_diff_assoc() - Computes the difference of arrays with additional index check, Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.

Array
   (
      [1] => blue
   )
72%

< ? php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2); ?
>

Array
   (
      [1] => blue
   )
65%

Remove an Element from an Array by its Value Using unset() , Remove an Element from an Array by its Key Using unset() , Using array_splice() to Delete Multiple Consecutive Elements from an Array ,The easiest way to remove or delete a single element from an array in PHP is to use the unset() function. This function is used to unset a given variable. In other words, you can use it to destroy any variable in your code.

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
   "third" => "Kelly", "fourth" => "Megan",
   "fifth" => "Henry"
];
load more v
75%

Also, there is a presentation with given code examples, so you can download it from the related links and show it to your colleagues to build a stronger team.,There are two different ways of creating arrays. One is to use array() to specify the elements as key-value pairs. The other method is to put all elements inside []. There are two important points that you should remember when creating associate arrays with key pairs.,You can see the combinations of these letters in the following table:,To get a part of an array—for example, just the first three elements—use array_slice():

Here are a few examples of creating arrays in PHP:

$first = array(10, "Apple", 20, -18, "Monkey");
print_r($first);
/* Array
(
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
) */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r($second);
/* Array
(
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
) */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r($third);
/* Array
(
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
) */
load more v
40%

Remove Multiple Elements from an Array Using array_splice() , Remove the Last Element from Array Using array_pop() , Remove First Element from Array Using array_shift() ,Let's try removing two elements from an array using array_splice():

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

array_shift($fruit);

print_r($fruit);
load more v

Other "removes-array" queries related to "Array_diff() removes too many elements"