Home Backend Development PHP Problem How to put numbers into array in php

How to put numbers into array in php

Apr 26, 2023 am 10:32 AM

In PHP, we can use arrays to store and process large amounts of data. Putting numbers into an array is also a very basic operation. Here are some methods you can use:

  1. Use "[]" syntax

In PHP, we can use "[]" syntax to put numbers into an array. This syntax is very simple and practical. The following is an example:

$arr = [];  // 创建一个空数组

$arr[] = 1;  // 将数1放进数组中
$arr[] = 2;  // 将数2放进数组中
$arr[] = 3;  // 将数3放进数组中

print_r($arr);  // 输出数组的内容
Copy after login

The above code will output the following results:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Copy after login
Copy after login
Copy after login

We can see that using the "[]" syntax can easily put numbers into an array , and this syntax is very simple.

  1. Use the "array_push()" function

In PHP, we can also use the "array_push()" function to put numbers into an array. This function adds one or more numbers to the end of an array. The following is an example:

$arr = [];  // 创建一个空数组

array_push($arr, 1);  // 将数1放进数组中
array_push($arr, 2);  // 将数2放进数组中
array_push($arr, 3);  // 将数3放进数组中

print_r($arr);  // 输出数组的内容
Copy after login

The above code will output the following results:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Copy after login
Copy after login
Copy after login

We can see that using the "array_push()" function can also easily put numbers into an array , and this function is very simple.

  1. Use the "array_unshift()" function

In PHP, we can also use the "array_unshift()" function to put numbers into an array. This function adds one or more numbers to the beginning of an array. The following is an example:

$arr = [];  // 创建一个空数组

array_unshift($arr, 3);  // 将数3放进数组中
array_unshift($arr, 2);  // 将数2放进数组中
array_unshift($arr, 1);  // 将数1放进数组中

print_r($arr);  // 输出数组的内容
Copy after login

The above code will output the following results:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Copy after login
Copy after login
Copy after login

We can see that using the "array_unshift()" function can also easily put numbers into an array , and this function is very practical. It allows us to add numbers at the beginning of the array instead of only at the end.

To sum up, you can easily put numbers into an array using the above three methods. These methods are very simple and practical, and can make us more convenient when processing data. Using arrays allows us to better store and process data, so learning how to put numbers into arrays is also a very important skill.

The above is the detailed content of How to put numbers into array in php. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)