How to delete array of element in array

daffodil

I have two array. I want to remove if 2nd array exists in 1st array. For example

array1 = array ("apple","banana","papaya","watermelon","avocado");
array2 = array ("apple","avocado");

I want the output should be

Array ( [1] => banana [2] => papaya [3] => watermelon)

Here are some code that I'd tried.

foreach($array2 as $key){
    $keyToDelete = array_search($key, $array1);
    unset($array1[$keyToDelete]);
}
print_r($array1);

but the output is

Array ( [1] => banana [2] => papaya [3] => watermelon [4] =>avocado )

It only remove first element.

i also tried to do something like this

$result = array_diff($array1,$array2);
print_r($result);

but the output is it print all element in array1

Noted: I want the result need to be outside foreach loop

Shivendra Singh

array_diff should be work.

<?php

$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");

$array_diff = array_diff($array1, $array2);

print_r($array_diff);

?>

DEMO

output will be.

Array ( [1] => banana [2] => papaya [3] => watermelon)

You can also try below solution. result will be same.. using in_array Check if first array value not in the second tester that value in the new array 'final_result' for results.

in_array support (PHP 4, PHP 5, PHP 7)

$array1 = array ("apple","banana","papaya","watermelon","avocado");
$array2 = array ("apple","avocado");

$final_result = array();
foreach($array1 as $value){

    if(!in_array($value, $array2)){

        $final_result[] = $value;
    }

}

print_r($final_result);

?>

DEMO

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to delete an element in an array with RethinkDB

分類Dev

How to delete an element from an array in C#

分類Dev

delete an array element inside array of array

分類Dev

Delete Array Element From Multidimensional Infinity Array

分類Dev

delete specific element in an array of local storage

分類Dev

delete element from non-associatve array

分類Dev

How to remove array element in mongodb?

分類Dev

How to remove whitespace in array element?

分類Dev

Delete element from multi-dimensional numpy array by value

分類Dev

How to delete negative duplicates from an array?

分類Dev

How to correctly delete an object from an array by name?

分類Dev

How to delete items from array in .txt C

分類Dev

How to delete particular no of elements from array in mongodb

分類Dev

how to delete array value from table in jquery

分類Dev

How to find and delete the duplciates on array of json

分類Dev

How to delete duplicate elements in a multidimensional array?

分類Dev

Delete an array value by another array

分類Dev

How to append array element with increasing the array count with key in PHP?

分類Dev

How to find an element in an array based on the value of an array porp

分類Dev

How to store number of object element of array in another array?

分類Dev

how to return every second element in a java array

分類Dev

How to append an element to an array column of a Spark Dataframe?

分類Dev

How to remove an element from an array in Swift

分類Dev

How to append an element to an array column of a Spark Dataframe?

分類Dev

How to pass an Array as element's Attribute?

分類Dev

how to get random element of array which is not null?

分類Dev

How to toggle any element in array in vuejs

分類Dev

How to get the size of an element inside an array?

分類Dev

How do I update the state of a array element?

Related 関連記事

  1. 1

    How to delete an element in an array with RethinkDB

  2. 2

    How to delete an element from an array in C#

  3. 3

    delete an array element inside array of array

  4. 4

    Delete Array Element From Multidimensional Infinity Array

  5. 5

    delete specific element in an array of local storage

  6. 6

    delete element from non-associatve array

  7. 7

    How to remove array element in mongodb?

  8. 8

    How to remove whitespace in array element?

  9. 9

    Delete element from multi-dimensional numpy array by value

  10. 10

    How to delete negative duplicates from an array?

  11. 11

    How to correctly delete an object from an array by name?

  12. 12

    How to delete items from array in .txt C

  13. 13

    How to delete particular no of elements from array in mongodb

  14. 14

    how to delete array value from table in jquery

  15. 15

    How to find and delete the duplciates on array of json

  16. 16

    How to delete duplicate elements in a multidimensional array?

  17. 17

    Delete an array value by another array

  18. 18

    How to append array element with increasing the array count with key in PHP?

  19. 19

    How to find an element in an array based on the value of an array porp

  20. 20

    How to store number of object element of array in another array?

  21. 21

    how to return every second element in a java array

  22. 22

    How to append an element to an array column of a Spark Dataframe?

  23. 23

    How to remove an element from an array in Swift

  24. 24

    How to append an element to an array column of a Spark Dataframe?

  25. 25

    How to pass an Array as element's Attribute?

  26. 26

    how to get random element of array which is not null?

  27. 27

    How to toggle any element in array in vuejs

  28. 28

    How to get the size of an element inside an array?

  29. 29

    How do I update the state of a array element?

ホットタグ

アーカイブ