PHP array() function

In this chapter you will learn:

  1. Description for PHP array() function
  2. Syntax for numeric indexed arrays
  3. Syntax for associative arrays
  4. Example for array() function
  5. Example - Creates an associative array named $age
  6. Example - loop through PHP array
  7. Example - loop associative array
  8. Example - multidimensional array

Description

In PHP, there are two types of arrays:

TypeMeaning
Indexed arraysArrays with numeric index values
Associative arraysArrays with named keys(key value pair)

The array() function is used to create an array of above types.

Syntax for numeric indexed arrays

Syntax to use array() function to create numeric indexed arrays is as follows.

array(value1,value2,value3,etc.);

Syntax for associative arrays

Syntax to use array() function to create associative arrays is as follows.

array(key=>value,key=>value,key=>value,etc.);
ParameterDescription
keythe key (numeric or string)
valuethe value

Example 1

Create an indexed array named $names, assign four elements to it, and then print a text containing the array values.


<?php
$names = array("Volvo","BMW","Toyota","PHP from java2s.com");
echo $names[0] . ", " . $names[1] . " and " . $names[3] . ".";
?>

The code above generates the following result.

Example 2

Creates an associative array named $age.


<?php
$age=array("PHP"=>"5","Python"=>"7","Java"=>"4");
echo "PHP is " . $age['PHP'] . " years old.";
?>

The code above generates the following result.

Example 3

Loop through and print all the values of an indexed array


<?php//from   ja  va 2  s .  c  om
$cars=array("A","B","C");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++){
  echo $cars[$x];
  echo "\n";
}
?>

The code above generates the following result.

Example 4

Loop through and print all the values of an associative array.


<?php/*  ja  va 2  s.com*/
$age=array("PHP"=>"5","Python"=>"7","Java"=>"3");

foreach($age as $x=>$x_value){
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "\n";
}
?>

The code above generates the following result.

Example 5

Create a multidimensional array.


<?php/*from   java  2s  .  c o m*/
$cars=array(
  array("Name"=>"PHP","Price"=>100),
  array("Name"=>"Python","Price"=>100),
  array("Name"=>"Java","Price"=>10)
);
var_dump($cars);
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP array_change_key_case() function
  2. Syntax for PHP array_change_key_case() function
  3. Parameter for PHP array_change_key_case() function
  4. Return value for PHP array_change_key_case() function
  5. Example for array_change_key_case() function
  6. Example - If two or more keys will be equal after running array_change_key_case()
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function