PHP Array
Posted on: November 3, 2009 at 12:00 AM
Advertisement
PHP Array: In this tutorial you will come to know about PHP array, several examples will help you to know array. Examples will help you to learn numeric array, associative array etc.

Array in PHP

Almost every language supports array, which is a collection of similar data type, but in PHP language it could be or could not be the same. An array could be a collection of similar or dissimilar datatype. 

PHP Array Example 1:

<?php

echo"An array with similar datatype"."<br/>";

$array=array("These","are","similar");

var_dump($array);

echo"An array with dissimilar datatype"."<br/>";

$array=array("String",122,34.34,true,'v');

var_dump($array);

?>

Output: 

An array with similar datatype
array(3) {
  [0]=>
  string(5) "These"
  [1]=>
  string(3) "are"
  [2]=>
  string(7) "similar"
}
An array with dissimilar datatype
array(5) {
  [0]=>
  string(6) "String"
  [1]=>
  int(122)
  [2]=>
  float(34.34)
  [3]=>
  bool(true)
  [4]=>
  string(1) "v"
}

Array in PHP with Example 2:

<?php

echo"An array with similar datatype"."<br/>";

$array=array("These","are","similar");

var_dump($array);

echo"var_dump() is a function which is used to print the elements of an array";

echo "<br/>Another function is print_r(), which is used to print an array<br/>";

print_r($array);

?>

Output:

An array with similar datatype
array(3) {
  [0]=>
  string(5) "These"
  [1]=>
  string(3) "are"
  [2]=>
  string(7) "similar"
}
var_dump() is a function which is used to print the elements of an array
Another function is print_r(), which is used to print an array
Array
(
    [0] => These
    [1] => are
    [2] => similar
)

Example 3:

<?php

echo"This is an example of numeric array<br/>";

$array=array("This","is","numeric","array");

var_dump($array);

echo"This is an example of associative array<br/>";

$array=array(1=>"This","s"=>2,3=>23.23);

var_dump($array);

?>

Output:

This is an example of numeric array
array(4) {
  [0]=>
  string(4) "This"
  [1]=>
  string(2) "is"
  [2]=>
  string(7) "numeric"
  [3]=>
  string(5) "array"
}
This is an example of associative array
array(3) {
  [1]=>
  string(4) "This"
  ["s"]=>
  int(2)
  [3]=>
  float(23.23)
}


Example 4:

<?php

echo "This is an example of numeric array<br/>";

$array=array( "string"=>array( "This", "is", "multidimensional", "array"),

"numeric"=>array(1,2,2,223,34));

var_dump ($array);

?>

Output:

This is an example of numeric array
array(2) {
  ["string"]=>
  array(4) {
    [0]=>
    string(4) "This"
    [1]=>
    string(2) "is"
    [2]=>
    string(14) "multidimensional"
    [3]=>
    string(5) "array"
  }
  ["numeric"]=>
  array(5) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(2)
    [3]=>
    int(223)
    [4]=>
    int(34)
  }
}

Note: To know array in more details please visit our web page: http://roseindia.net/tutorial/php/phpbasics/index.html

 

 

Advertisement

Related Tags for PHP Array:


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.

Posted on: November 3, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com