How to create an array in PHP, including variable names and their values

WBOY
Release: 2024-03-19 13:26:01
forward
769 people have browsed it

php editor Xinyi introduces you how to create an array in PHP. In PHP, you can use the array() function to create an array that includes variable names and their values. For example, you can create an array containing variable names and values ​​like this: $colors = array("red", "blue", "green"); This creates an array named $colors, which contains three values. : red, blue and green. In this way, you can easily create and manipulate arrays in PHP, enabling more flexible and efficient programming.

PHP Build Array

In php, an array is an ordered collection of key-value pairs . You can create an array using the array() function or the square bracket syntax [].

Use array() function

array() function accepts a set of key-value pairs as parameters and returns an associative array. For example:

$my_array = array( "name" => "John Doe", "age" => 30, "city" => "New York" );
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Use square bracket syntax []

You can also use square bracket syntax [] to create an array. In this case, the key names are automatically assigned based on the order of the elements in the array. For example:

$my_array = ["John Doe", 30, "New York"];
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Accessing array elements

Array elements can be accessed like ordinary variables. If you know the key name, you can use square bracket syntax. For example:

echo $my_array["name"]; // Output "John Doe"
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
If you don't know the key names, you can use a foreach loop to iterate through the array. For example:

foreach ($my_array as $key => $value) { echo "$key: $value
"; }
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Add and remove elements

You can add or remove elements to an array using the following methods:

  • array_push(): Push one or more elements to the end of the array.
  • array_unshift(): Adds one or more elements to the beginning of the array.
  • array_pop(): Remove the last element in the array.
  • array_shift(): Remove the first element in the array.
For example:

array_push($my_array, "London"); // Add "London" to the end of the array array_unshift($my_array, "Paris"); // Add "Paris" to the beginning of the array array_pop($my_array); // Remove the last element in the array
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Other array functions

PHP provides many other array functions, including:

  • count(): Returns the number of elements in the array.
  • array_keys(): Returns the list of keys in the array.
  • array_values(): Returns a list of values ​​in an array.
  • in_array(): Check whether a value is in the array.
  • array_merge(): Merge two or more arrays.
By using these functions, you can easily create, manage, and manipulate arrays in PHP.

The above is the detailed content of How to create an array in PHP, including variable names and their values. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!