Categories: php

PHP Arrays

An array is a data structure that stores one or more similar type of values in a single value.

For example, if you were to store 100 numbers, then instead of defining 100 variables, it is easy to define an array of 100 length. Each value in the array is accessed using an id, which is called array index.

See arrays in Java

Three (3) Kinds of Array

  1. Numeric Array: This refers to an array with a numeric index. Values are stored and accessed in a linear fashion.
  2. Associative Array: This refers to an array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  3. Multi-dimensional Array: This refers to an array that contains one or more arrays. Values are accessed using multiple indices.

Numeric Array

These arrays can store numbers, strings and any object, but their index will be represented by numbers. By default the array index starts from 0.

<?php
$num = array("1", "2", "3", "4", "5");
foreach($num as $val){
echo $val;
}
?>

Associative Array

Associative arrays are similar to numeric arrays in terms of functionality, but they are different in terms of their index.

Associative arrays will have their index as string so that a strong association between key and values is established.

<?php
$salary = array(
           "John" => 2000,
           "James" => 1000,
           "Zara" => 2500,
           );
echo "John's salary is ".$salary['John'];
//echo for the others
?>

Multi-dimensional Array

A multi-dimensional array is one in which each element in the main array can also be an array, and each element in the sub-array can be an array as well. And it goes on and on.

Values in the multi-dimensional array are accessed using multiple index.

<?php
$marks = array("John" => array("Physics" => 30, "Math" => 20, "English" => 10), "Musa" => array("Physics" => 10, "Math" => 10, "English" => 10));

echo "John's score in physics is ".$marks['John']['Physics'];
?>

See PHP documentation for arrays

Recent Posts

South Korea bans downloads of DeepSeek AI until further notice

The South Korean government announced on Monday that it had temporarily halted new downloads of…

1 day ago

Which programming language is best for software development?

As a software developer, choosing the right programming language for software development can be a…

4 days ago

What is a server farm?

A server farm, also known as a server cluster or data center, is a collection…

5 days ago

Elon Musk’s Starlink satellite internet service is awaiting authorization to begin operations in Pakistan.

Pakistan's mobile and broadband internet speeds rank in the bottom 10 percent globally, according to…

1 week ago

React Native Styling Guide

React Native is a popular framework for building cross-platform mobile applications using JavaScript and React.…

1 week ago

You can now customize your Android home screen shortcut with any widget of your choice

Google is not only passionate about developing innovative apps and services but also about finding…

2 weeks ago