遍历数组并按索引添加到另一个数组
Looping through arrays and adding by index to another array
我有一个如下所示的数组:
array(8) {
["rentalPropertyAddress"]=>
array(15) {
[0]=>
string(11) "111 tree st"
[1]=>
string(11) "112 tree st"
[2]=>
string(11) "122 tree st"
}
["gasInitialized"]=>
array(15) {
[0]=>
string(2) "on"
[1]=>
string(2) "on"
[2]=>
string(3) "off"
}
["waterInitialized"]=>
array(15) {
[0]=>
string(3) "off"
[1]=>
string(2) "on"
[2]=>
string(2) "on"
}
["electricInitialized"]=>
array(15) {
[0]=>
string(2) "on"
[1]=>
string(2) "on"
[2]=>
string(3) "off"
}
["inspectionDate"]=>
array(15) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
}
["rentalDate"]=>
array(15) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
}
["vacantInitialized"]=>
array(15) {
[0]=>
string(2) "no"
[1]=>
string(2) "no"
[2]=>
string(3) "yes"
}
}
我需要做的是将每个数组的每个索引添加到另一个或多个数组中。例如,预期输出为:
array {
array {
[0] => string(11) "111 tree st"
[1] => string(2) "on"
[2] => string(3) "off"
[3] => string(2) "on"
[4] => string(0) ""
[5] => string(0) ""
[6] => string(2) "no"
}
...
}
我已经尝试通过遍历数组并保留它的索引来使用 forloop 来执行此操作:
$i = -1;
$retval = array();
foreach ($_GET as $key => $item) {
$i += 1;
$retval[$i] = $item[$i];
}
echo "<pre>";var_dump($retval);
但是输出不是我所期望的:
array(8) {
[0]=>
string(11) "111 tree st"
[1]=>
string(2) "on"
[2]=>
string(2) "on"
[3]=>
string(3) "off"
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(3) "yes"
[7]=>
string(1) "5"
}
如何才能成功地将数组中的数据提取到不同的数组中?
如果我没有误解你需要的输出 那么你可以使用 two foreach()
来迭代每个嵌套数组和按所有嵌套数组的索引位置推送每个元素。
$_GET = [
"rentalPropertyAddress"=>["111 tree st","112 tree st","122 tree st"],
"gasInitialized"=>["on","on","off"],
"waterInitialized"=>["off","on","on"],
"electricInitialized"=>["on","on","off"],
"inspectionDate"=>["","",""],
"rentalDate"=>["","",""],
"vacantInitialized"=>["no","no","yes"]
];
$retval = [];
foreach ($_GET as $key => $item) {
$i=0;
foreach($item as $k=>$v){
$retval[$i][] = $v;
$i++;
}
}
echo "<pre>";
print_r($retval);
echo "</pre>";
将列切换为行的一般模式是:
foreach ($a as $row) {
foreach ($row as $col => $value) {
$result[$col][] = $value;
}
}
但是,您可以在超全局中获得此结果,而无需使用 PHP 通过重命名您的表单输入(假设数据来自表单)来自 [=13] =]
<input type="text" name="rentalPropertyAddress[]">
<input type="text" name="rentalPropertyAddress[]">
为这种格式
<input type="text" name="properties[0][rentalPropertyAddress]">
<input type="text" name="properties[1][rentalPropertyAddress]">
即使您使用 JS 动态添加行,这仍然是可能的。
我建议对 运行 7.1 到 7.3 的 php 版本执行以下操作(例如,参见演示 link):
/*The initial array : I chose this one as it is simpler than the one in the question and I find it better for illustration purposes*/
/*Considering the question, we will only get the values ended by 0 (first value of each subarray )*/
$arrays = [['00','01'],['10','11'],['20','11']];
/*Initialisation of the result array*/
$indexes = [];
/*We are going to use the new syntax for list() function introduced in php 7.1 (See the credits link for list() function usage examples )*/
/* We pull the first value (index 0) of each subarray that we store in the result array ($indexes) */
foreach($arrays as $array){
list('0'=>$indexes[]) = $array;
}
var_dump($indexes); // ['00','10','20']
制作人员:The list function & practical uses of array destructuring in PHP
如果你穿着袜子,你可能想挂在袜子上。您正在寻找的技术称为 "array transposing"。只要您删除外部关联键,array_map()
和一个 splat 运算符 (...
) 将快速准备您的数据。
var_export(array_map(null,...array_values($_GET)));
是的,这确实简单。 Demo
如果您遵循 Don'tPanic 的建议并以 html 形式准备您的数据结构,唯一比这更容易的方法。
输出:
array (
0 =>
array (
0 => '111 tree st',
1 => 'on',
2 => 'off',
3 => 'on',
4 => '',
5 => '',
6 => 'no',
),
1 =>
array (
0 => '112 tree st',
1 => 'on',
2 => 'on',
3 => 'on',
4 => '',
5 => '',
6 => 'no',
),
2 =>
array (
0 => '122 tree st',
1 => 'off',
2 => 'on',
3 => 'off',
4 => '',
5 => '',
6 => 'yes',
),
)
我有一个如下所示的数组:
array(8) {
["rentalPropertyAddress"]=>
array(15) {
[0]=>
string(11) "111 tree st"
[1]=>
string(11) "112 tree st"
[2]=>
string(11) "122 tree st"
}
["gasInitialized"]=>
array(15) {
[0]=>
string(2) "on"
[1]=>
string(2) "on"
[2]=>
string(3) "off"
}
["waterInitialized"]=>
array(15) {
[0]=>
string(3) "off"
[1]=>
string(2) "on"
[2]=>
string(2) "on"
}
["electricInitialized"]=>
array(15) {
[0]=>
string(2) "on"
[1]=>
string(2) "on"
[2]=>
string(3) "off"
}
["inspectionDate"]=>
array(15) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
}
["rentalDate"]=>
array(15) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
}
["vacantInitialized"]=>
array(15) {
[0]=>
string(2) "no"
[1]=>
string(2) "no"
[2]=>
string(3) "yes"
}
}
我需要做的是将每个数组的每个索引添加到另一个或多个数组中。例如,预期输出为:
array {
array {
[0] => string(11) "111 tree st"
[1] => string(2) "on"
[2] => string(3) "off"
[3] => string(2) "on"
[4] => string(0) ""
[5] => string(0) ""
[6] => string(2) "no"
}
...
}
我已经尝试通过遍历数组并保留它的索引来使用 forloop 来执行此操作:
$i = -1;
$retval = array();
foreach ($_GET as $key => $item) {
$i += 1;
$retval[$i] = $item[$i];
}
echo "<pre>";var_dump($retval);
但是输出不是我所期望的:
array(8) {
[0]=>
string(11) "111 tree st"
[1]=>
string(2) "on"
[2]=>
string(2) "on"
[3]=>
string(3) "off"
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(3) "yes"
[7]=>
string(1) "5"
}
如何才能成功地将数组中的数据提取到不同的数组中?
如果我没有误解你需要的输出 那么你可以使用 two foreach()
来迭代每个嵌套数组和按所有嵌套数组的索引位置推送每个元素。
$_GET = [
"rentalPropertyAddress"=>["111 tree st","112 tree st","122 tree st"],
"gasInitialized"=>["on","on","off"],
"waterInitialized"=>["off","on","on"],
"electricInitialized"=>["on","on","off"],
"inspectionDate"=>["","",""],
"rentalDate"=>["","",""],
"vacantInitialized"=>["no","no","yes"]
];
$retval = [];
foreach ($_GET as $key => $item) {
$i=0;
foreach($item as $k=>$v){
$retval[$i][] = $v;
$i++;
}
}
echo "<pre>";
print_r($retval);
echo "</pre>";
将列切换为行的一般模式是:
foreach ($a as $row) {
foreach ($row as $col => $value) {
$result[$col][] = $value;
}
}
但是,您可以在超全局中获得此结果,而无需使用 PHP 通过重命名您的表单输入(假设数据来自表单)来自 [=13] =]
<input type="text" name="rentalPropertyAddress[]">
<input type="text" name="rentalPropertyAddress[]">
为这种格式
<input type="text" name="properties[0][rentalPropertyAddress]">
<input type="text" name="properties[1][rentalPropertyAddress]">
即使您使用 JS 动态添加行,这仍然是可能的。
我建议对 运行 7.1 到 7.3 的 php 版本执行以下操作(例如,参见演示 link):
/*The initial array : I chose this one as it is simpler than the one in the question and I find it better for illustration purposes*/
/*Considering the question, we will only get the values ended by 0 (first value of each subarray )*/
$arrays = [['00','01'],['10','11'],['20','11']];
/*Initialisation of the result array*/
$indexes = [];
/*We are going to use the new syntax for list() function introduced in php 7.1 (See the credits link for list() function usage examples )*/
/* We pull the first value (index 0) of each subarray that we store in the result array ($indexes) */
foreach($arrays as $array){
list('0'=>$indexes[]) = $array;
}
var_dump($indexes); // ['00','10','20']
制作人员:The list function & practical uses of array destructuring in PHP
如果你穿着袜子,你可能想挂在袜子上。您正在寻找的技术称为 "array transposing"。只要您删除外部关联键,array_map()
和一个 splat 运算符 (...
) 将快速准备您的数据。
var_export(array_map(null,...array_values($_GET)));
是的,这确实简单。 Demo
如果您遵循 Don'tPanic 的建议并以 html 形式准备您的数据结构,唯一比这更容易的方法。
输出:
array (
0 =>
array (
0 => '111 tree st',
1 => 'on',
2 => 'off',
3 => 'on',
4 => '',
5 => '',
6 => 'no',
),
1 =>
array (
0 => '112 tree st',
1 => 'on',
2 => 'on',
3 => 'on',
4 => '',
5 => '',
6 => 'no',
),
2 =>
array (
0 => '122 tree st',
1 => 'off',
2 => 'on',
3 => 'off',
4 => '',
5 => '',
6 => 'yes',
),
)