PHP- Arrays

Arrays are used for store more than one element or data on a single variable. We can store all types of data with in a single array. Array can be represented using two ways that are array with open bracket and array with closed bracket. The data is stored with in the bracket.

  1. array( data )
  2. [ data ]

 

The programmer can find the length of the array using the function count.

Example 1 [ simple array and its count ] 

Output

 

There is an another example for array.

Example 2

Output

  • For, foreach are the two loop functions which help us to repeatedly go through an array.

 

PHP Array types

Arrays are divided into three based on its characteristics

  • Indexed array
  • Associative array
  • Multidimensional array

 

Indexed arrays

  • Arrays contain index values for simplify the array searching process.
  • The array which contain numerical value as index then these kinds of arrays are known as indexed arrays.

 

Example 1 [ Indexed array]

Output

 

Example 2 [ Indexed array ]

output

 

For loop function help us to repeatedly go through an indexed array and print all the values stored in the array.

Example 3 [ loop through an indexed array ]

Output

 

Associative arrays

  • In indexed arrays we use numerical values as keys.
  • In case of associative array we use numerical 0r string values as keys.
  • The name given to the keys are unique, that is do not give same name to two different data.

 

Example 1 [ associative array ]

Output

 

Example 2 [ associative array ]

Output

  • Foreach loop function help us to repeatedly go through an associative array and print all the values stored in the array.

 

Example 3 [ loop through associative array ]

Output

 

Multidimensional array

If an array contain another one or more arrays then these types of arrays are known as multidimensional arrays.

Output

 

Example 1 [ loop through Multidimensional array ]

Output

 

Summary
Article Name
PHP- Arrays
Description
Tutorial about php arrays. indexed array, associative array, multidimensional array.
Author
Publisher Name
Jees K Denny
Publisher Logo

Leave a Reply