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

PHP how to fill an array with specified keys and values

WBOY
WBOYforward
2024-03-19 15:52:05908browse

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"];

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";

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");

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

Method 4: Use array_merge() function

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

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

Method 5: Use $array[] syntax

$array = [];
$array[] = "value1";
$array["key2"] = "value2";

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

?>

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
)

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:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete