- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- PHP program to find the first ‘n’ numbers that are missing in an array
- Find four missing numbers in an array containing elements from 1 to N in Python
- Find four missing numbers in an array containing elements from 1 to N in C++
- PHP program to find the numbers within a given array that are missing
- Removing duplicate elements from an array in PHP
- Program to find kth missing positive number in an array in Python
- Golang Program to Remove Repeated Elements From an Array
- Swift Program to Remove Duplicate Elements From an Array
- Golang program to remove all elements from an array
- Golang Program To Remove Duplicate Elements From An Array
- 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 minimum element in an array
- PHP program to find the maximum element in an array
- Swift Program to fetch elements from an array based on an index

Advertisements