What You Should Know About Arrays in PHP - Coding Cyber

What You Should Know About Arrays in PHP

What You Should Know About Arrays in PHP

Other Articles in PHP Advanced Data Types Section

Quickly Navigate to Other Sections in PHP Tutorials

PHP Basics
PHP Data Types
PHP Advanced Data Types
PHP Logical Statements
PHP Operators
PHP Loops
User Defined Functions in PHP
Useful PHP Functions

Array: Array’s are used to store multiple values in a single variable. These arrays are used in retrieving data from the database.

A simple example of Array :

Like this above example, arrays are used to store lot of data.

Arrays in PHP are used to store multiple values inside an array. There are three types of arrays.

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more arrays Syntax of an array

In this syntax, we are just adding the values to array. There are no pocket numbers, but they can be accessed from zero, one, two, three and so on… First value is always assigned to pocket values zero. These are called as Indexed arrays.

array(value, value2, value3, … )

In this syntax, we are adding values to array with accessible key. We can always access exact value with key name or number, whatever we assign to it. These are called as Associative arrays.

array(
key => value,
key2 => value2,
key3 => value3,

)

Example code of an array

This is an example code for Indexed array. In this type of array, we will just add values to cars array. And then we can access them based on the pocket numbers. Array pocket number start from zero.

$cars = array(“Volvo”, “BMW”, “Toyota”);
echo “I like ” . $cars[0] . “, ” . $cars[1] . ” and ” . $cars[2] . “.”;

Related Posts
No related posts for this content
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: