Table of Contents
Syntax
Parameters
Return
Example
输出
Home Backend Development PHP Tutorial array() function in PHP

array() function in PHP

Aug 20, 2023 pm 07:49 PM
array function php array array() function

array() function in PHP

The array() function in PHP creates an array.

Array is of three types in PHP.

  • Indexed arrays − It is an array with numeric index

  • Associative arrays − It is an array with named keys

  • Multidimensional arrays − It is an array that have one or more arrays

Syntax

// array with numeric index i.e. Indexed arrays
array(value1,value2...);
// array with named keys i.e. associative arrays
array(key1 => value1, key2 => value2...)
Copy after login

Parameters

  • value − Set the value.

  • key − Set the key.

Return

The array() function returns an array of the parameters.

The following is an example of indexed arrays.

Example

 Live Demo

<?php
$products = array("Electronics","Clothing","Accessories", "Footwear");
$len = count($products);
for($i = 0;$i<$len;$i++) {
   echo $products[$i];
   echo "<br>";
}
?>
Copy after login

输出

Electronics
Clothing
Accessories
Footwear
Copy after login

The following is an example of associative arrays.

Example

 Live Demo

<?php
$rank = array("Football"=>"1","Cricket"=>"2");
foreach($rank as $mykey=>$myvalue) {
   echo "Key = " . $mykey . ", Value = " . $myvalue;
   echo "<br>";
}
?>
Copy after login

输出

Key = Football, Value = 1
Key = Cricket, Value = 2
Copy after login

The above is the detailed content of array() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Array functions in PHP8: efficient use of array_pad() Array functions in PHP8: efficient use of array_pad() May 16, 2023 pm 02:00 PM

Array functions in PHP8: efficient use of array_pad()

Array functions in PHP8: multiple uses of array_unique() Array functions in PHP8: multiple uses of array_unique() May 17, 2023 am 08:13 AM

Array functions in PHP8: multiple uses of array_unique()

Quickly extract JSON data from PHP arrays Quickly extract JSON data from PHP arrays Apr 30, 2024 pm 03:54 PM

Quickly extract JSON data from PHP arrays

Detailed explanation of how to use array_diff_key() of PHP array function Detailed explanation of how to use array_diff_key() of PHP array function Jun 27, 2023 pm 05:18 PM

Detailed explanation of how to use array_diff_key() of PHP array function

How to use Go language array function to sum and return the result? How to use Go language array function to sum and return the result? Jul 31, 2023 pm 02:25 PM

How to use Go language array function to sum and return the result?

Array functions in PHP8: various operating techniques of array_slice() Array functions in PHP8: various operating techniques of array_slice() May 15, 2023 pm 10:43 PM

Array functions in PHP8: various operating techniques of array_slice()

PHP8.1 update: performance improvements for array and string functions PHP8.1 update: performance improvements for array and string functions Jul 08, 2023 am 08:25 AM

PHP8.1 update: performance improvements for array and string functions

Array functions in PHP8: Examples of various operations of array_intersect_assoc() Array functions in PHP8: Examples of various operations of array_intersect_assoc() May 18, 2023 am 08:13 AM

Array functions in PHP8: Examples of various operations of array_intersect_assoc()

See all articles