Put values ​​into two-dimensional array in PHP loop

不言
Release: 2023-03-24 18:36:02
Original
1355 people have browsed it

The content of this article is to put values ​​into a two-dimensional array in a PHP loop. It has a certain reference value. Now I share it with you. Friends in need can refer to it

1. Define a Array, put the value into the two-dimensional array

$a = [];$b = ['a','b','c','d'];foreach($b as $v){    $a['a'][] = $v;
}
Copy after login

In this way, printing this $a will get a two-dimensional array, but when using the following code, $a will get It can only get one value,

instead of putting all the values ​​in $b into $a['a']this array

$b = ['a','b','c','d'];foreach($b as $v){    $a = [];    $a['a'][] = $v;
}
Copy after login

At the same time, in While loops and for loops are the same, when defining an array, it must be placed outside the loop, otherwise

can only put the value of the last loop into this array

Explanation: Each loop $a is redefined once, so this phenomenon will occur.

Related recommendations:

In-depth understanding of php loop statements

The above is the detailed content of Put values ​​into two-dimensional array in PHP loop. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!