- 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 Array Operators
Introduction
PHP defines following set of symbols to be used as operators on array data types
Symbol | Example | Name | Result |
---|---|---|---|
+ | $a + $b | Union | Union of $a and $b. |
== | $a == $b | Equality | TRUE if $a and $b have the same key/value pairs. |
=== | $a === $b | Identity | TRUE if $a and $b have the same key/value pairs in the same order and of the same types. |
!= | $a != $b | Inequality | TRUE if $a is not equal to $b. |
<> | $a <> $b | Inequality | TRUE if $a is not equal to $b. |
!== | $a !== $b | Non-identity | TRUE if $a is not identical to $b. |
Union of arrays
The Union operator appends the right-hand array appended to left-hand array. ; If a key exists in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
Following example shows use of define() function to define constants
Example
<?php $arr1=array("phy"=>70, "che"=>80, "math"=>90); $arr2=array("Eng"=>70, "Bio"=>80,"CompSci"=>90); $arr3=$arr1+$arr2; var_dump($arr3); ?>
Output
Following result will be displayed
array(6) { ["phy"]=> int(70) ["che"]=> int(80) ["math"]=> int(90) ["Eng"]=> int(70) ["Bio"]=> int(80) ["CompSci"]=> int(90) }
comparison of arrays
Two arrays are said to be equal if they have same key-value pairs. Following example has an indexed array and other associative array with keys corresponding to index of elements in first. Hence both are equal
Example
<?php $arr1=array(0=>70, 2=>80, 1=>90); $arr2=array(70,90,80); var_dump ($arr1==$arr2); var_dump ($arr2!=$arr1); ?>
Output
Following result will be displayed
bool(true) bool(false)
Identity operators
Arrays are identical if and only if both of them have same set of key-value pairs and in same order
Example
<?php $arr1=array(0=>70, 1=>80, 2=>90); $arr2=array(70,90,80); var_dump ($arr1===$arr2); $arr3=[70,80,90]; var_dump ($arr3===$arr1); ?>
Output
Following result will be displayed
bool(false) bool(true)
- Related Articles
- PHP String Operators
- PHP Type Operators
- PHP Increment/Decrement Operators
- ‘AND’ vs ‘&&’ operators in PHP
- Relational Operators on STL Array in C++
- PHP Associative Array
- PHP Indexed Array
- PHP Multidimensional Array.
- array() function in PHP
- Sort php multidimensional array by sub-value in PHP
- How to convert PHP array to JavaScript array?
- PHP: Remove object from array
- Find maximum in an array without using Relational Operators in C++
- Find minimum in an array without using Relational Operators in C++
- How to convert Multidimensional PHP array to JavaScript array?
