What is an Array? Functions of Array- Array Tutorials

0saves

What is an Array?

An array can be used when we want a single variable to hold multiple values. Values in arrays can be accessed by using the index associated to it. Each element in the array has its own index. Default start of index starts from 0.

In PHP, there are three kinds of arrays:

?Numeric array – An array with a numeric index. Default index starts from 0.

?Associative array – An array where each ID key is associated with a value

?Multidimensional array – An array containing one or more arrays

Numeric Arrays

A numeric array stores each array element with a numeric index.

Example

 <?php

$colors = array("red","green","blue");

for($i=0; $i<sizeof($colors); $i++) {

echo $colors[$i]."<br>";

}

/*

Output :

red
 green
 blue

*/

?> 

Associative Arrays

An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them.

Example

 <?php

$colors = array("sky"=>"blue", "tomato" =>"red", "grass"=>"green");  // (Define Keys)

foreach ($colors as $key => $color) {

echo $key."=>".$colors[$key]."<br>";

}

/*

Output :

sky=>blue
 tomato=>red
 grass=>green

*/

?> 

Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example

 <?php

$colors = array

(

"Grass"=>array

(

"Green",

"Brown"

),

"Rose"=>array

(

"Red",

"Yellow"

),

"Sand"=>array

(

"Black",

"Yellow"

)

);

print_r($colors);

/*

Output:

Array

(              [Grass] => Array

( [0] => Green [1] => Brown )

[Rose] => Array

( [0] => Red [1] => Yellow )

[Sand] => Array

( [0] => Black [1] => Yellow )

)

*/

?> 

Most Frequently Used Array Functions

sort()

  • This function sorts an array in ascending order.
  • If numeric array is sorted the index values are reordered and the data of array is sorted
  • If associated array is sorted then the index value of array is lost and the new index value starting from 0 is assigned after sorting.

Example 1:

 <?php

$colors = array("red","green","blue");

sort($colors);

for($i=0; $i<sizeof($colors); $i++) {

echo $colors[$i]."<br>";

}

/*

Output :

blue
 green
 red

*/

?> 

Example 2:

 <?php

$colors = array("sky"=>"blue", "tomato" =>"red", "grass"=>"green");  // (Define Keys)

sort($colors);

foreach ($colors as $key => $color) {

echo $key."=>".$colors[$key]."<br>";

}

/*

Output :

0=>blue

1=>green

2=>red

*/

?> 

rsort()

It is a sorting function for arrays in PHP which sorts an array in descending order of the values.

asort()

The asort() function sorts an array by the values. The values keep their original keys.

Example 1:

 <?php

$colors = array("sky"=>"blue", "tomato" =>"red", "grass"=>"green");  // (Define Keys)

asort($colors);

foreach ($colors as $key => $color) {

echo $key."=>".$colors[$key]."<br>";

}

/*

Output :

sky=>blue

grass=>green

tomato=>red

*/

?> 

arsort()

The asort() function sorts an array by the values in descending order. The values keep their original keys.

ksort()

The ksort() function sorts an array by the keys. The values keep their original keys.

Example

 <?php

$colors = array("sky"=>"blue", "tomato" =>"red", "grass"=>"green");  // (Define Keys)

ksort($colors);

foreach ($colors as $key => $color) {

echo $key."=>".$colors[$key]."<br>";

}

/*

Output :

grass=>green

sky=>blue

tomato=>red

*/

?> 

krsort()

The ksort() function sorts an array by the keys in descending order. The values keep their original keys.

sizeof()

The sizeof() function counts the elements of an array, or the properties of an object. This function is an alias of the count() function.

Example

 <?php

$colors = array("red","green","blue");

echo sizeof($colors);

/*

Output

3

*/

?> 

print_r(<array>); //

The print_r() function print elements of array with keys

Example

 <?php

$colors = array("red","green","blue");

echo print_r($colors);

/*

Output

Array ( [0] => red [1] => green [2] => blue )

*/

?> 


You can download this tutorial in pdf format by clicking here
Comments and Suggestions are always welcome. Please share it with your friends and colleagues if you think this tutorial is useful.
Reference : w3schools.com, php.net, google.com

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Related Posts

About the Author: Abhishek Sanghvi

Hi Friends, my name is Abhishek Sanghvi and I am the founder of this “open source script” site MYPHPSCRIPTZ. I have been learning and practising PHP from the last 3 years. You could always contact me if you wish to have some code for your need. I would surely try and solve them for you at abhishek(at)myphpscriptz(dot)com

6 Comments + Add Comment

Leave a comment


+ one = four

CommentLuv badge