

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program to find missing elements from an array
The ‘array_diff’ function can be used to find the elements that are missing from the array.
Example
<?php function absent($my_list) { $my_array = range(min($my_list), max($my_list)); return array_diff($my_array, $my_list); } echo "Elements missing from first array are "; print_r(absent(array(45, 48, 51, 52, 53, 56))); echo "Elements missing from second array are "; print_r(absent(array(99, 101, 104, 105))); ?>
Output
Elements missing from first array are Array ( [1] => 46 [2] => 47 [4] => 49 [5] => 50 [9] => 54 [10] => 55 ) Elements missing from second array are Array ( [1] => 100 [3] => 102 [4] => 103 )
A function named ‘absent’ is defined that checks to see the minimum number and the maximum number and generates an array within that range. The function then returns the difference between this array and the original array, using the ‘array_diff’ function, that gives the missing elements from the array.
- Related Questions & Answers
- Find four missing numbers in an array containing elements from 1 to N in C++
- Find four missing numbers in an array containing elements from 1 to N in Python
- Removing duplicate elements from an array in PHP
- PHP program to find the first ‘n’ numbers that are missing in an array
- PHP program to find the numbers within a given array that are missing
- Program to find kth missing positive number in an array in Python
- Program to find the kth missing number from a list of elements in Python
- Program to find maximum product of two distinct elements from an array in Python
- PHP program to find the maximum element in an array
- PHP program to find the minimum element in an array
- C program to find the unique elements in an array.
- Finding the nth missing number from an array JavaScript
- How to delete elements from an array?
- PHP program to find standard deviation of values within an array
- C# Program to skip elements of an array from the end
Advertisements