Share on Google+Share on Google+

PHP Array Type

Advertisement
In this current PHP tutorial we will study about PHP array type and the examples will help you to learn array more precisely.

PHP Array Types:

In PHP an array is more than just array, it is an ordered map. A map is a type that we can associate values to keys. We can consider it as an array, list or vector, hash table, etc. 

In associative array a key may be a string or an integer, for example "5" will be considered as 5 where "05" will be considered as "05". Float in key will be truncated as integer.

Following examples will help you to learn array quickly and effectively.

PHP Array Example:

<?php

$array=array("Hello","New","World");

echo count($array);

sort($array);

for($i=0;$i<=4;$i++){

echo $array[$i]."<br/>";

}

$array=array("Hello","Hi","Hei");

sort($array);

for($i=0;$i<=4;$i++){

echo $array[$i]."<br/>";

}

$array=array("Hello"=>1,"Hi"=>2,"Hei"=>3);

asort($array);

foreach($array as $key=>$value){

echo "Key: ".$key."Value: ".$value."<br/>";

}

echo "<br/>";

$array=array("Hello"=>1,"Hi"=>2,"Hei"=>3);

ksort($array);

foreach($array as $key=>$value){

echo "Key: ".$key."Value: ".$value."<br/>";

}

?>

 

Output:

3Hello
New
World


Hei
Hello
Hi


Key: HelloValue: 1
Key: HiValue: 2
Key: HeiValue: 3

Key: HeiValue: 3
Key: HelloValue: 1
Key: HiValue: 2

Advertisements

Advertisement

Posted on: March 16, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.