How to use php hash array

WBOY
Release: 2023-05-05 22:42:06
Original
565 people have browsed it

PHP is a popular server-side scripting language, especially in web development. In PHP, hash array is a very useful data structure that helps us store and access data easily. This article will introduce the use of hash arrays in PHP.

1. Definition and characteristics of hash array

Hash array, also known as associative array, is a data structure that uses key-value pairs to store and access data. In PHP, a hash array is a data structure based on a hash table, which can use strings as keys and any type of data as values.

The characteristics of the hash array include:

  1. The hash array is an unordered data structure and elements cannot be obtained through subscripts.
  2. The hash array supports dynamic expansion and can automatically adjust the capacity to adapt to data storage needs.
  3. The time complexity of the search and insertion operations of the hash array is O(1), which is very efficient.

2. Creation and initialization of hash array

Creating a hash array using PHP is very simple. You can use the array() function to create an empty hash array. You can also use the argument syntax of an associative array, similar to the object literal in JavaScript, to initialize and assign a hash array.

For example:

// 创建空的哈希数组
$hashArray = array();

// 初始化和赋值哈希数组
$hashArray2 = array(
  "name" => "张三",
  "age" => 18,
  "email" => "zhangsan@example.com"
);
Copy after login

In the above example, $hashArray is an empty hash array; $hashArray2 is a hash array containing three key-value pairs, namely "name ", "age" and "email".

3. Basic operations of hash array

Hash array supports a series of basic operations, including adding elements, getting elements, modifying elements, deleting elements and traversing elements. These operations are described in detail below.

  1. Add elements

To add elements to the hash array, you can use the following syntax:

$hashArray["key"] = "value";
Copy after login

where "key" is the key name , "value" is the key value. For example:

$hashArray2["gender"] = "男";
Copy after login

In this way, $hashArray2 becomes a hash array containing four key-value pairs.

  1. Get elements

To get the elements in the hash array, you can use the following syntax:

$value = $hashArray["key"];
Copy after login

Among them, "key" is to get The key name of the element, $value is the value of the element obtained. For example:

$name = $hashArray2["name"];
Copy after login

In this way, $name is assigned the value "Zhang San".

  1. Modify elements

To modify the elements in the hash array, you can use the following syntax:

$hashArray["key"] = "newValue";
Copy after login

Among them, "key" is to be modified The key name of the element, "newValue" is the new value. For example:

$hashArray2["age"] = 20;
Copy after login

In this way, the value "18" in $hashArray2 is modified to "20".

  1. Delete elements

To delete elements from the hash array, you can use the following syntax:

unset($hashArray["key"]);
Copy after login

Where "key" is to be deleted The key name of the element. For example:

unset($hashArray2["email"]);
Copy after login

In this way, the "email" key-value pair in $hashArray2 is deleted.

  1. Traverse elements

To traverse the elements in a hash array, you can use a foreach loop. The foreach loop can iterate over each element in the array. The syntax is as follows:

foreach ($hashArray as $key => $value) {
  // 迭代到每个元素,$key是键名,$value是键值
  echo $key . ": " . $value . "<br>";
}
Copy after login

For example:

foreach ($hashArray2 as $key => $value) {
  echo $key . ": " . $value . "<br>";
}
Copy after login

In this way, each key-value pair in $hashArray2 will be printed out.

4. Advanced operations of hash arrays

In addition to basic operations, hash arrays also support some advanced operations, including obtaining a key name list, obtaining a key value list, and determining whether a key name is Existing and calculating the length of a hash array, etc.

  1. Get the key list

To get the list of all keys of a hash array, you can use the array_keys() function, the syntax is as follows:

$keys = array_keys($hashArray);
Copy after login

Among them, $keys is an array containing all key names. For example:

$keys2 = array_keys($hashArray2);
Copy after login

In this way, $keys2 is assigned an array containing four elements, namely "name", "age", "email" and "gender".

  1. Get the key value list

To get all the key value list of a hash array, you can use the array_values() function, the syntax is as follows:

$values = array_values($hashArray);
Copy after login

Among them, $values ​​is an array containing all key values. For example:

$values2 = array_values($hashArray2);
Copy after login

In this way, $values2 is assigned an array containing four elements, namely "Zhang San", 20, null and "Male".

  1. Determine whether a key name exists

To determine whether a key name exists in a hash array, you can use the array_key_exists() function, the syntax is as follows:

$exists = array_key_exists("key", $hashArray);
Copy after login

Among them, "key" is the key name to be judged, and $exists is a Boolean value indicating whether the key name exists in the hash array. For example:

$exists2 = array_key_exists("email", $hashArray2);
Copy after login

In this way, the value of $exists2 is false because the "email" key-value pair has been deleted from $hashArray2.

  1. Calculate the length of a hash array

To get the length of a hash array, you can use the count() function, the syntax is as follows:

$length = count($hashArray);
Copy after login

Among them, $length is the length of the hash array. For example:

$length2 = count($hashArray2);
Copy after login

In this way, $length2 is 3, because a set of key-value pairs has been deleted from $hashArray2.

5. Conclusion

Hash array is a very commonly used data structure in PHP, which can help us store and access data easily. In this article, we introduce the definition and characteristics of hash arrays in PHP, and provide a detailed explanation of its basic and advanced operations. I hope this article can help you better understand and apply hash arrays.

The above is the detailed content of How to use php hash array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template