PHP Array Length: How to Find Array Length in PHP [With Examples]

What Are PHP Arrays?

  • In PHP, we implement the array of an assigned Map.
  • A map is a conceptual data type of key-value pairs, we can consider it as an interface.
  • We can implement a map in various ways For Example: HashTable, HashMap, Dictionary.
  • To know the depth of understanding, We can check the PHP source for Array and HashTable.
  • We can use the array to save the combination of data as per the scenarios and assign them to a particular variable name.
  • Each array item we can assign as a key and value pair.

Below is the example we can consider as key and value pair:

Source

Note: we categorize Arrays as “indexed array” and “associative array” based on the key stipulation. The Indexed array has a default index that starts with ‘0’.The associative array includes the user-defined key index. We can use the keys for strings and natural numbers.

How to create an Array in PHP?

Example:

  • An empty array

<?php

$emptyArray = array();

?>

  • Single-dimensional array

<?php

$animals = array(“Bear”, “Leopard”, “Tiger”);

$arrLength = count($animals);

// loop through the array

for($i = 0; $i < $arrLength; $i++) {

    echo $animals[$i];

    echo “</ br>”;

}

?>

  • Associative array

<?php

$animals = array(“Leopard”=>”Wild”, “Cow”=>”Domestic”, “Lion”=>”Wild”);

// loop through associative array and get key-value pairs

foreach($animals as $key => $value) {

    echo “Key=” . $key . “, Value=” . $value;

    echo “</br>”;

}

?>

  • Two-dimensional array

<?php

//two-dimensional array definition declaration

$animals = array

  (

  array(“Leopard”,”Wild”,8),

  array(“Cow”,”Domestic”,12),

  array(“Lion”,”Wild”,20)

  );

// two-dimensional array iteration declaration

for ($row = 0; $row < 3; $row++) {

  echo “<p>Row number $row</p>”;

  echo “<ul>”;

  for ($col = 0; $col < 3; $col++) {

    echo “<li>”.$animals[$row][$col].”</li>”;

  }

  echo “</ul>”;

}

?>

  • Via loop

<?php

$animals = array();

for ($i = 0; $i < $count; $i++) {

    $animals[$i] = array

        ( $animalName[$i]

        , $animalType[$i]

        );

}

?>

  • Three-dimensional array

<?php

$threeDArray = array( 

    array( 

        array(“Leopard”, “Lion”), 

        array(“Cow”, “Cat”), 

    ), 

    array( 

        array(“Mango”, “Grapes”), 

        array(“Cauliflower”, “Potato”), 

    ), 

); 

?>

Below is the image as a perfect example for the three-dimensional array:

Source

PHP | sizeof() Function

The sizeof() function is a built-in function in PHP, and we can use it to count the number of elements present in an array countable object.

Syntax:

int sizeof(array, mode);

Parameter: As per the above syntax example, this function accepts two parameters.

  • array: This parameter defines the array which contains elements which we need to count.
  • mode: This is an optional parameter, and here we specify the mode of the function. It can take two types of values as per the below:
    • 0: It is default, does not count all elements of multidimensional arrays
    • 1: It Counts the array recursively (It counts all the elements with multidimensional arrays)

How to Check if a Value Exists in an Array in PHP

Check if the value exists in an array in PHP

Step 1 – Use the PHP in_array() function to test if a value exists in an array or not. 

Step 2 – Define the in_array() function 

As per the below code snippet Example:

<?php

$zoo = array(“Leopard”, “Tiger”, “Elephant”, “Zebra”, “Rhino”, “Dear”);

if(in_array(“Elephant”, $zoo)){

    echo “The elephant was found in the zoo.”;

}

echo “<br>”;

if(in_array(“Tiger”, $zoo)){

    echo “The tiger was found in the zoo.”;

}

?>

Also Read: OOPS Concepts in PHP

How to Count all Elements or Values in an Array in PHP

We can use the PHP count() or sizeof() function to get the particular number of elements or values in an array.

  • The count() and sizeof() function returns 0 for a variable that we can initialize with an empty array.
  • If we do not set the value for a variable, it returns 0.

Below is the code snippet:

<?php

$days = array(“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”); 

// Printing array size

echo count($days);

echo “<br>”;

echo sizeof($days);

?>

How to Print or Echo all the Values of an Array in PHP

Use the PHP foreach loop.

  • For printing the array values here we have different ways 
  • The easiest method we have foreach loop

As per the below example, we are repeating the $colors array and print all its elements using the echo or print statement.

Below is the code snippet:

<?php

$colors = array(“Yellow”, “Purple”, “Red”, “Brown”, “Skyblue”);

// Loop through colors array

foreach($colors as $value){

    echo $value . “<br>”;

}

?>

How to Display Array Structure and Values in PHP

Use the PHP print_r() or var_dump() Statement

  • For checking the structure and values of an array.
  • You can either use the PHP print_r() or var_dump() statement to see or check the structure and values of an array with an easily readable format on the screen.
  • The var_dump() statement provides better information than print_r().

Below is the code snippet example we can consider for this:

<?php

$cities = array(“Canada”, “Australia”, “New Jersey”); 

// Print the cities array

Print_r($cities);

echo “<hr>”;

var_dump($cities);

?>

How to Remove the Last Element From an Array in PHP

Use the PHP array_pop() function

  • For removing the particular value or element from the end of an array.
  • The array_pop() function returns the last value of an array. 
  • If the array is empty (without element value in an array list), the returned value will be NULL.

Below is the code snippet Example to explain how this function runs:

<?php

$sports = array(“Tennis”, “Cricket”, “BasketBall”, “Badminton”);

// Deleting last array item

$removed = array_pop($sports);

print_r($sports);

echo “<br>”;

var_dump($removed);

?>

Checkout: Career Opportunities in PHP

Conclusion

If you’re interested to learn more about PHP, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT
LEARN MORE

Leave a comment

Your email address will not be published.

×