How to add key-value pairs to php array

青灯夜游
Release: 2023-03-15 11:06:02
Original
7790 people have browsed it

How to add key-value pairs to a php array: 1. Use the "array_pad ($arr, $size, $value)" statement; 2. Use the "array_unshift ($arr, $value)" statement; 3. Use the "array_push($arr,$value)" statement.

How to add key-value pairs to php array

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php array add key Value pair

1. Use array_pad() function

array_pad() function can fill the array to the specified length with a certain value. The syntax is as follows:

array_pad ( $arr, $size , $value )
Copy after login

Parameter description:

  • arr represents the array to be filled.
  • size represents the length to be filled:
    • If size is a positive number, fill to the right side of the array;
    • If size is a negative number, start from the left Filling;
    • If the absolute value of size is less than or equal to the length of the arr array, there will be no filling.
  • value represents the value to be filled.

Example:

<?php
$arr = array(12, 10, 9);
var_dump($arr);
$result = array_pad($arr, 5, 0);  //从数组右侧开始, 用0填补数组到含有5个元素
var_dump($result);
$result = array_pad($arr, -7, -1);  //从数组左侧开始, 用-1填补数组到含有7个元素
var_dump($result);
$result = array_pad($arr, 2, "noop");  // 第二个参数小于数组长度, 不填补
var_dump($result);
?>
Copy after login

How to add key-value pairs to php array

#2. Use array_unshift() function

array_unshift() function Used to insert one or more elements at the beginning of the array, the syntax is as follows:

array_unshift ( array &$arr , mixed $value1 [, mixed $value2, mixed $value3 ... ] )
Copy after login

Two points to note:

  • The elements are inserted as a whole , these elements will remain in the same order after insertion.

  • All numeric key names will be modified to start counting from 0, and all string key names will remain unchanged.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$num = array(10, 45, 9);
var_dump($num);
array_unshift($num, 100, 6);  //在数组开头插入元素
var_dump($num);
?>
Copy after login

How to add key-value pairs to php array

##3. Use the array_push() function

array_push() function is used to insert one or more elements (push) at the end of the array. Its syntax is as follows:

array_push ( array &$arr , mixed $value1 [, mixed $value2, mixed $value3 ... ] )
Copy after login

Example:


<?php
header("Content-type:text/html;charset=utf-8");
$num = array(10, 45, 9);
var_dump($num);
array_push($num, 100, 6);  //在数组开头插入元素
var_dump($num);
?>
Copy after login

How to add key-value pairs to php array

Recommended learning: "

PHP Video Tutorial", "PHP ARRAY"

The above is the detailed content of How to add key-value pairs to php array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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