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 Data Types : Arrays

Description

An array in PHP is a collection of key/value pairs. This means that it maps values to keys. Array keys (or indexes) may be either integers or string whereas values can be any type.

Array() construct

An array can be declared using the array() language construct, which generally takes the following format.

array( key1=> value1, 
key2=> value3, 
key3=> value3, 
.......... )

Key1, key2, key3 may be an integer or string.

value1, value2, value3 may be any value of any type.

As of PHP 5.4 a short array syntax [] is used instead of array().

Example:

<?php
$fruits = array(
fruit1 => "Banana",
fruit2 => "Apple" 
);
// declaring the above array as of PHP 5.4
$fruits = [
fruit1 => "Banana",
fruit2 => "Apple" 
];
?>

In the above example, Banana and Apple are the values and fruit1, fruit2 are the keys of the array $fruits.

Indexed and Associative Arrays

In PHP there is two kinds of arrays : indexed array and associative array. The only difference is that numeric values are used as 'keys' in indexed array start from zero (0) and in associative array, strings are used as 'keys'. PHP does not differentiate between indexed and associative arrays, therefore a PHP array may contain strings as well as integers as 'keys'.

Example : Indexed arrays with key

<?php
$fruits[0]="Banana";
$fruits[1]="Apple";
$fruits[2]="Mango";
$fruits[3]="Coconut";  
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(6) "Banana" [1]=> string(5) "Apple" [2]=> string(5) "Mango" [3]=> string(7) "Coconut" }

Here var_dump() function is used to display structured information of an array.

Example : Indexed arrays without key

<?php
$fruits = array("Banana", "Apple", "Mango", "Coconut");
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(6) "Banana" [1]=> string(5) "Apple" [2]=> string(5) "Mango" [3]=> string(7) "Coconut" }

In PHP array key is optional. If no key is specified, keys start from zero (0).

Example : Integer and string keys together

<?php
$fruits=array(
0 => "Banana",
"fruit1" => "Apple",
11 => "Mango",
-34 => "Coconut",
);
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(7) "Banana" ["fruit1"]=> string(5) "Apple" [11]=> string(5) "Mango" [-34]=> string(7) "Coconut" } 

Example: Keys are not present on all elements

<?php
$fruits=array(
"Banana",
11=>"Apple",
"Mango",
"fruit1" => "Coconut",
);
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(6) "Banana" [11]=> string(5) "Apple" [12]=> string(5) "Mango" ["fruit1"]=> string(7) "Coconut" }

In the above example the third value " Mango " is assigned the key 12 because the largest integer key before that was 11.

Storing Data in an Array

Storing a value in an array is easy, you can use any of the following method to store date:

<?php
//Store data through an Indexed array.
$country[0] = 'India'; 
$country[1] = 'USA'; 
$country[2] = 'Peru';
//Store data through an associative array.
$price['country1'] = 'India';
$price['country2'] = 'USA';
$price['country3'] = 'Peru';
?>

Accessing array elements

The elements of an array can be accessed using the array[key] syntax, see the following example.

<?php
$fruits=array(
0 => "Banana",
"fruit1" => "Apple",
11 => "Mango",
-34 => "Coconut",
);
echo($fruits[0]);
echo($fruits["fruit1"]);
echo($fruits[11]);
echo($fruits[-34]);
?>

Output :

 BananaAppleMangoCoconut

Multidimensional array

A multidimensional array is a structure which holds various arrays in an array.

Here is an example.

<?php
$class = array
(
"ClassV" => array
(
"David", 
"Alex",
"Emma"
),
"ClassVI" => array
(
"Tyler",
"Ryan",
"Killian"
),
"ClassVII" => array
(
"Liliana",
"Dante",
"Zaira"
)
);
?>

Accessing multidimensional array elements

<?php
$class = array
("ClassV" => array
("David", 
"Alex",
"Emma"
),
"ClassVI" => array
("Tyler",
"Ryan",
"Killian"
)
);
// Print the second name of the student from class V.
echo($class["ClassV"][1]);
// Output - Alex
?>

Previous: Strings
Next: Objects



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