
Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
Arrays in Category: PHP by amit
🕙 Posted on 2023-05-14 at 08:45:42 Read in Hindi ...
Array
Array is a compound data-type in many programming languages, for example, C, C++, Java, PHP, JavaScript, Python, etc. It can hold many items of different data-types, including an array, or other compound data-type. In Python, an array is known as list. In every programming languages, there are different built-in (pre-defined) functions, specifically for array data-type. These functions are also known as methods or properties.
In PHP, an array data type can be defined in two ways: (1) with array()
function (reserved keyword), and (2) with []
(two square brackets). Both are same and have no difference between them. As of PHP 5.4, the latter way(2) to define an array has also been adopted, and it is called as short syntax. The funda is to 'write less, do more'. You will also learn about many pre-defined functions, specifically used in PHP for arrays.
As you have read in previous page, a computer does whatever instruction is given to it. Programmers and web-developers have to script everything in the CMS (Content Management System) for a website, or other programmes, Apps, etc. It means that to add more features in your website, you have to write more and more. This may be tedious tasks, when you alone have to type everything from your keyboard!
<?php
var_dump( array()
); // You cannot display a compound data-type with echo function
echo '<br />'; print_r( []
); // print_r() function is used to display data in human-readable way ?>
Array ( )
array()
or []
is also called empty array, i.e., there are no item inside them. You can check them with empty()
function. 0
(zero), 0.0 (floating point number), ""
(a pair of double/"0"
, false, and array()
or []
are evaluated as empty. However, "0.0", " "
(a blank space within a pair of double/
Also, these two representation of an array, is called as array literals. When you run the above example, more memory locations are allocated to these two representations of array, as compared to memory locations allocated to boolean, integer or string data-types. But, these array literals are still not assigned to any variable name or constant name, in above example.
<?php
$my_var = array()
; // an empty array is assigned to variable, $my_var
var_dump( empty
( $my_var ) ); // outputs bool(true)
echo '<br />'; echo empty
( []
); // outputs 1?>
Let's insert some item into an Array
You can place items of any data-type into an array, directly assigned to a variable name or a constant name. Each item in an array should be separated with , (comma). The value for an item inside an array, can either be integer, float, string, array, boolean, or an object. When there is only one item inside an array, you should not put , (comma). Both of following arrays are identical, and there is no difference between them.
<?php $my_var = array
( "Amit", 'Krishna', true
, 1, 2.5 ); ?>
<?php $my_var = [
"Amit", 'Krishna', true
, 1, 2.5 ]
; ?>
REMEMBER: You cannot directly display an array, with echo
function, and you have to use var_dump()
or print_r()
functions, to display it on the web-browser. You can use echo '<pre>'; and echo '</pre>';, before and after displaying an array with print_r()
function, respectively. This will enclose the output into the <pre> </pre> HTML element.
<?php
$my_var = array
( "Amit", 'Krishna', true
, 1, 2.5 );
echo '<pre>';
print_r
( $my_var );
echo '</pre>'; ?>
(
[0] => Amit
[1] => Krishna
[2] => 1
[3] => 1
[4] => 2.5
)
</pre>
The above output can be viewed in source-code by pressing Ctrl U keys simultaneously, when the result is displayed in the web-browser. You can see that third item of the array, in above example, true
is displayed as 1. Let't check the above example of same array with var_dump()
function. You can uncomment following two lines above and below the var_dump( $my_var ); which will output the <pre> </pre> HTML element.
<?php
$my_var = array
( "Amit", 'Krishna', true
, 1, 2.5 );
// echo '<pre>';
var_dump
( $my_var );
// echo '</pre>';
echo '<br />'; echo count
( $my_var ); ?>
5
The above output shows that the array is of length 5, which is returned from the pre-defined count()
function. It is specifically used for arrays, to count all elements (items) inside an array. It always returns an integer value for all items in a specific array.
Two type of Arrays
Arrays are of two types: (1) Index based arrays, and (2) Associative arrays. In official documentation of PHP, you will find that an array is (actually) an ordered map. A map is a type that associates values to keys. It means that an item (or an element) inside an array is the value, which is associated with a key. You can write the items inside an array, in above example, as follows:
<?php $my_var = array
( 0 => "Amit", 1 => 'Krishna', 2 => true
, 3 => 1, 4 => 2.5 );
var_dump
( $my_var ); ?>
In the above illustration, you can see in the first line, that inside the parentheses of array()
, first item is linked with 0
(zero - first whole number), second item is linked with 1
, and so on... by the =>
(arrow operator, without any blank space between the symbols). Back to the funda, 'write less, do more', when these integers (positive whole numbers) are not written by the programmer, the PHP interpreter will automatically assign them. These are referred as keys, associated with respective items.
In PHP, these keys can either be of decimal integer data-type (not binary, octal or hexadecimal), or string data-type. You can write these keys in other scalar data-types, for example, boolean, float, null, etc. When you write any other whole number, for example, 20
instead of 2, the latter key doesn't exist. You cannot access value of item associated with key 2. Also, when you append a new item into that array, next key will be one more than greatest integer (whole number).
<?php $my_var = array
( 0 => "Amit", 1 => 'Krishna', 20 => true
, 3 => 1, 4 => 2.5 );
var_dump
( $my_var );
echo $my_var[2]; // shows ERROR ?>
Warning: Undefined array key 2 in C:\xampp\
You will learn more about how to access, append, delete an item or any value from that item, in next page. You will also learn more about Associative Array, and other complex (NESTED) arrays, pre-defined functions specifically for arrays, etc. in next pages.
Leave a Comment:

Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤

Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇