Home > Backend Development > PHP Tutorial > How to add values ​​to all one-dimensional arrays in a two-dimensional array in php

How to add values ​​to all one-dimensional arrays in a two-dimensional array in php

墨辰丷
Release: 2023-03-28 06:54:02
Original
2658 people have browsed it

This article mainly introduces the method of PHP to add values ​​to all one-dimensional arrays in a two-dimensional array, involving PHP's array traversal, conversion, assignment and other related operating skills. Friends who need it can refer to it

The details are as follows:

Add values ​​(indexes and associations) to all one-dimensional arrays in the two-dimensional array

$shop = array(
  0=>array(0=>1,1=>2,2=>3,3=>4)
  ,1=>array(0=>1,1=>2,2=>3)
  ,2=>array(0=>1,1=>2,2=>3)
  );
print_r($shop);
//示例 1:引用循环变量的地址赋值
foreach($shop as &$shoplist){
  $shoplist[] = '4444444444444';
  $shoplist['we'] = '欢迎光临脚本之家';
}
print_r($shop);
Copy after login

Running results:

Array (
[0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临脚本之家 )
[1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 )
[2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 )
)
Copy after login
Copy after login

//示例2:修改循环变量数组,重新赋值
foreach($shop as $key=>$shoplist){
  $index = count($shoplist);
  $shoplist[$index] = '4444444444444';
  $shoplist['we'] = '欢迎光临脚本之家';
  $shop[$key]=$shoplist;
}
print_r($shop);
Copy after login

Running results:

Array (
[0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临脚本之家 )
[1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 )
[2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 )
)
Copy after login
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php implements the method of adding values to all one-dimensional arrays in a two-dimensional array

pinovke-Ask me about the problem that occurs when C# connects mysql through MySQLDriverCS and adds values ​​ to the table

##Jquery implements clicking the button to continuously add the value

to the textarea Example code_jquery

The above is the detailed content of How to add values ​​to all one-dimensional arrays in a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

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