TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PHP Array

PHP array for beginners and professionals with examples, php file, php session, php date, php array, php form, functions, time, xml, ajax, php mysql, regex, string, oop

<< Back to PHP

PHP Arrays

PHP array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar type in a single variable.


Advantage of PHP Array

Less Code: We don't need to define multiple variables.

Easy to traverse: By the help of single loop, we can traverse all the elements of an array.

Sorting: We can sort the elements of array.


PHP Array Types

There are 3 types of array in PHP.

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

PHP Indexed Array

PHP index is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default.

There are two ways to define indexed array:

1st way:

$season=array("summer","winter","spring","autumn");

2nd way:

$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";

Example

File: array1.php
<?php
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>

Output:

Season are: summer, winter, spring and autumn
File: array2.php
<?php
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>

Output:

Season are: summer, winter, spring and autumn
Click me for more details...

PHP Associative Array

We can associate name with each array elements in PHP using => symbol.

There are two ways to define associative array:

1st way:

$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");

2nd way:

$salary["Sonoo"]="350000";
$salary["John"]="450000";
$salary["Kartik"]="200000";

Example

File: arrayassociative1.php
"350000","John"=>"450000","Kartik"=>"200000");  
echo "Sonoo salary: ".$salary["Sonoo"]."
"; echo "John salary: ".$salary["John"]."
"; echo "Kartik salary: ".$salary["Kartik"]."
"; ?>

Output:

Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
File: arrayassociative2.php
";
echo "John salary: ".$salary["John"]."
"; echo "Kartik salary: ".$salary["Kartik"]."
"; ?>

Output:

Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
Click me for more details...

PHP Multidimensional Array

Click me for more details...
Next TopicPHP Indexed Array




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf