Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

PHP: Array Operators

Description

This is a Comprehensive PHP array operators tutorial from w3resource.com

List of array operators

Name Example Result
Union $x + $y Union of $x and $y. The + operator appends elements of remaining keys from the right-sided array to the left-handed, but duplicated keys are not overwritten.
Equality $x == $y TRUE if $x and $y have the same key/value pairs.
Identity $x === $y TRUE if $x and $y have the same key/value pairs in the same order and of the same types.
Inequality $x != $y TRUE if $x is not equal to $y.
Inequality $x <> $y TRUE if $x is not equal to $y.
Non-identity $x !== $y TRUE if $x is not identical to $y.

Example : array union (+) operator

In the following example, the union operator adds the last element from the $b array ($c = $a + $b) with $a array as "c=>" key is not present in $a array. In the second statement ($c = $b + $a) no element is added from $b as all the keys of $a array are present in $b array.

<?php 
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b : <br />";
var_dump($c);
$c = $b + $a; // Union of $b and $a 
echo "<br />Union of \$b and \$a : <br />";
var_dump($c);
?>

Output:

Union of $a and $b: 
array(3) {    ["a"]=>    string(5) "apple"    ["b"]=>    string(6) "banana"    ["c"]=>    string(6) "cherry"  } 
Union of $b and $a :
array(3) {    ["a"]=>    string(4) "pear"    ["b"]=>    string(10) "strawberry"    ["c"]=>    string(6) "cherry"  } 

View the example in the browser

Example : array equality (==) and identity(===) operators

In the following example equality operator returns true as the two arrays have same key/value pairs whereas identity operator returns false as the key/value of the comparing arrays are same but not in same order.

<?php
$a = array("1" => "apple", "0" => "banana");
$b = array( "banana", "apple");
var_dump($a == $b);
var_dump($a === $b);
?>

Output:

bool(true)  bool(false) 

View the example in the browser

Previous: String Operators
Next: Incrementing Decrementing Operators



PHP: Tips of the Day

How to Sort Multi-dimensional Array by Value?

Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

Example:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
    return $a['order'] - $b['order'];
});

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
    return $a['order'] <=> $b['order'];
});

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
    $retval = $a['order'] <=> $b['order'];
    if ($retval == 0) {
        $retval = $a['suborder'] <=> $b['suborder'];
        if ($retval == 0) {
            $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
        }
    }
    return $retval;
});

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual

Ref : https://bit.ly/3i77vCC