Home » PHP

PHP array() Function with Example

PHP array() Function: Here, we are going to learn about the array() function with example in PHP.
Submitted by IncludeHelp, on February 07, 2019

PHP array() Function

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, ...);

PHP code:

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

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
)

PHP Code:

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

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.