Multi Dimensional Array in PHP | Associative Array in PHP

Array in PHP| Different Type of Array in PHP| How to use Array in PHP

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

PHP – Two-dimensional Arrays

It is the simplest form of a multidimensional array. It can be created using nested array. These type of arrays can be used to store any type of elements, but the index is always a number. By default, the index starts with zero.

Syntax

array (
    array (elements...),
    array (elements...),
    ...
)

Example:

<?php 
$marks = [
    "mohan" => ["maths" => 85, "sci" => 75, "sst" => 65],
    "sohan" => ["maths" => 74, "sci" => 78, "sst" => 12],
    "rohan" => ["maths" => 45, "sci" => 78, "sst" => 41],
];
foreach ($marks as $key => $v1) {
    echo $key . " ";
    foreach ($v1 as $v2) {
        echo $v2 . " ";
    }
    echo "<br>";
} 
?>

PHP sizeof() function

Php sizeof() function also returns the number of values.

In case of single array:

<?php 
$food = ["apple", "orange", "mango"];
echo sizeof($food); 
?>

Output

3

In case of multidimensional array:

<?php 
$food = [
    "fruits" => ["orange", "apple"],
    "veggies" => ["carrot", "raddish"],
];
echo sizeof($food); 
?>

Output:

2

Count all arrays of main array.

just change this in your previous code. This will count all arrays of $food variable.

<?php 
$food = [
    "fruits" => ["orange", "apple"],
    "veggies" => ["carrot", "raddish"],
];
echo sizeof($food, 1); 
?>

Output:

6

Count specific array of array.

For example. Count arrays of ‘fruits’.

<?php 
$food = [
    "fruits" => ["orange", "apple"],
    "veggies" => ["carrot", "raddish"],
];
echo sizeof($food[‘fruits’], 1); 
?>

Output:

2

How to use above code

<?php 
$food = ["apple", "orange", "mango"];
$len = count($food);
for ($i = 0; $i < $len; $i++) {
    echo $food[$i] . "<br>";
} 
?>

Output:

apple
orange
mango

Search PHP arrays

PHP search arrays are used to search arrays. For example, in the below code we want to search ‘mango’.

$a=[“apple”, “mango”, “guava”];

To do this, in PHP, we have 2 arrays named:

  • In_array
  • Array_search

If we use in_array, it will only give results as 0 and 1. O means False and 1 means True. If the given word is in the array function, the result will be 1. If the given word is not in the array, the result will be 0.

If we use the array_search function, it will give us the index of an array or its key.

If the array is the index, the result will print the index.

If an array is like an associative array, the result will print its key.

In_array

Example of in_array function:

<?php
$food = ["apple", "orange", "mango"];
if (in_array(‘apple’, $food)) {
    echo "found";
} else {
    echo "not found";
}
?>

Output:

found

Search integer number:

<?php 
$food = [45, "orange", "mango"];
if (in_array(45, $food)) {
    echo "found";
} else {
    echo "not found";
} 
?>

Output:

Found

If int value is written in quotes in array function:

<?php 
$food = [‘45’, "orange", "mango"];
if (in_array(45, $food, true)) {
    echo "found";
} else {
    echo "not found";
} 
?>

Explanation: if a user does not add a true parameter, the result will be ‘found’ even though 45 written in an array is a string, and in if function it is written as an integer because without quotes. So, if the user wants to search exact integer value in an array then it must write a true parameter.

In_array in multidimensional array:

<?php 
$food = [["a", "o"], ["p", "s"]];
if (in_array(["a", "o"], $food, true)) {
    echo "found";
} else {
    echo "not found";
} 
?>

Array_search

Array_search in indexed array: array_search in indexed array returns its index number.

<?php 
$food = ["apple", "orange", "mango"];
echo array_search("mango", $food); 
?>

Output:

2

Explanation: 0=apple, 1=orange, 2=mango.

Array_search in associative array

In associative array of array_search, the key of an array will be printed.

<?php 
$food = ["a" => "apple", "b" => "orange", "c" => "mango"];
echo array_search("mango", $food); 
?>

Output

C

Explanation: key of mango is ‘c’.

Multi Dimensional Array in PHP | Associative Array in PHP
Show Buttons
Hide Buttons