Home > Backend Development > PHP Tutorial > PHP how to fill an array with specified keys and values

PHP how to fill an array with specified keys and values

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2024-03-19 15:54:01
forward
1000 people have browsed it

php editor Xiaoxin will introduce how to fill an array with specified keys and values. In PHP, you can use the array_fill_keys() function to achieve this purpose. This function accepts two parameters, the first parameter is the array of keys and the second parameter is the value to be filled. With this function, you can easily create an array with specified keys and values. Next, we will detail how to use the array_fill_keys() function to fill an array.

Fill the array with the specified keys and values

In php, there are several ways to fill an array with specified keys and values:

Method 1: Use array literal syntax

$array = ["key1" => "value1", "key2" => "value2"];
Copy after login

This method creates an associative array where keys and values ​​are paired via the => operator.

Method 2: Add key-value pairs one by one

$array = [];
$array["key1"] = "value1";
$array["key2"] = "value2";
Copy after login

This method first creates an empty array, and then adds key-value pairs to the array one by one.

Method 3: Use Array() function

$array = array("key1" => "value1", "key2" => "value2");
Copy after login

This method creates an associative array using the Array() function.

Method 4: Use array_merge() function

$array = array_merge(["key1" => "value1"], ["key2" => "value2"]);
Copy after login

This method combines two or more associative arrays into a new associative array.

Method 5: Use $array[] syntax

$array = [];
$array[] = "value1";
$array["key2"] = "value2";
Copy after login

This method uses $array[] syntax to add values ​​to an array. If key is not specified, it will use consecutive integers as keys.

Notice:

  • For associative arrays, the keys must be strings or integers, while the values ​​can be of any data type.
  • Keys cannot be repeated. Subsequently added key-value pairs will overwrite the previous key-value pairs.
  • Both keys and values ​​in the array can use variables or expressions.

Example:

<?php

//Use array literal syntax
$array1 = ["name" => "John Doe", "age" => 30];

//Add key-value pairs one by one
$array2 = [];
$array2["name"] = "Jane Doe";
$array2["age"] = 25;

//Use Array() function
$array3 = array("name" => "Bob Smith", "age" => 40);

//Use array_merge() function
$array4 = array_merge(["name" => "Alice Miller"], ["age" => 35]);

// Use $array[] syntax
$array5 = [];
$array5[] = "Tom Johnson";
$array5["age"] = 28;

print_r($array1);
print_r($array2);
print_r($array3);
print_r($array4);
print_r($array5);

?>
Copy after login

Output:

Array
(
[name] => John Doe
[age] => 30
)
Array
(
[name] => Jane Doe
[age] => 25
)
Array
(
[name] => Bob Smith
[age] => 40
)
Array
(
[name] => Alice Miller
[age] => 35
)
Array
(
[0] => Tom Johnson
[age] => 28
)
Copy after login

The above is the detailed content of PHP how to fill an array with specified keys and values. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template