PHP Array array() Function (With Examples)

In this tutorial, we will learn about the PHP array() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array() Function

The array() function is a predefined function in PHP, it is used to create an array of the elements, array of the arrays etc.

By using array() function, we can create two types of arrays,

  1. Indexed arrays
  2. Associative arrays (arrays with the keys & values)
  3. And, multidimensional arrays

Syntax to create an indexed array:

array(element1, element2, element2, ...);

Syntax to create array with keys:

array(key1=>value1, key2=>value2, key3=>value3, ...);

Syntax

The syntax of the array() function:

array(mixed ...$values)

Parameters

The parameters of the array() function:

  • values: The values to create an array.

Return Value

The return type of this method is array, it returns an array of the given values.

Example 1: Use of PHP array() Function

Here, we are creating an indexed array and printing the array with one by one element (using indexes) and also printing the complete array.

<?php
//array of numbers
$arr_num = array(10, 20, 30, 40, 50);
//array with strings
$arr_string = array("Laptop", "Mobile", "Tablet");
//array with mixed types
$arr_mixed = array("Hello", 10, "friends", 20);
	
//printing arrays elements
print ("Array elements: $arr_num[0], $arr_num[1],  $arr_num[2],  $arr_num[3],  $arr_num[4]\n");
print ("Array elements: $arr_string[0], $arr_string[1],  $arr_string[2]\n");
print ("Array elements: $arr_mixed[0], $arr_mixed[1],  $arr_mixed[2],   $arr_mixed[3]\n");	
	
//printing complete arrays
print ("arr_num is...\n");
print_r ($arr_num);
print ("arr_string is...\n");
print_r ($arr_string);
print ("arr_mixed is...\n");
print_r ($arr_mixed);
?>

Output

The output of the above example is:

Array elements: 10, 20,  30,  40,  50
Array elements: Laptop, Mobile,  Tablet
Array elements: Hello, 10,  friends,   20
arr_num is...
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
arr_string is...
Array
(
    [0] => Laptop
    [1] => Mobile
    [2] => Tablet
)
arr_mixed is...
Array
(
    [0] => Hello
    [1] => 10
    [2] => friends
    [3] => 20
)

Example 2: Use of PHP array() Function

Here, we are creating associative (array with keys & values) and printing elements using keys and also printing the complete array.

<?php
//creating student array with keys & values
$std = array('id' => "101", 'name' => "Amit", 'course' => "B.Tech");

//printing elemenets
print ("std elements...\n");
print ("Id = " . $std['id'] . " Name = " . $std['name'] . " Course = " . $std['course']);
	
//printing complete array
print ("std...\n");
print_r($std);
?>

Output

The output of the above example is:

std elements...
Id = 101 Name = Amit Course = B.Techstd...
Array
(
    [id] => 101
    [name] => Amit
    [course] => B.Tech
)

To understand the above examples, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.