PHP 分解多维数组
PHP Explode multidimensional arrays
我有多个数组,我正试图分解并划分其中的所有信息。出于某种原因,当我这样做时,我丢失了有关主数组的信息。
数组
[0] => Array (
[0] => 2020-11-01 / 2020-11-30
[1] => 2020-12-01 / 2020-12-31
)
[1] => Array (
[0] => 2020-11-01 / 2020-11-30
[1] => 2020-12-01 / 2020-12-31
)
我使用了以下代码:
foreach ($list as $line) {
foreach ($line as $text) {
$parts[] = explode(' / ', $text);
}
}
print_r($parts) 将 return 以下内容:
[0] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[1] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
[2] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[3] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
但不幸的是,这不是我要找的,我需要的是:
[0] => Array (
[0] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[1] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
)
[1] => Array (
[0] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[1] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
)
我四处搜索,但没有找到一个简单的方法。希望有人能帮忙。谢谢!
如果您真的想保持原始阵列完好无损。一个解决方案是使用另一个数组将你的分解数组放在里面:
<?php
$list = [
[
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
],
[
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
]
];
$newArray = [];
foreach ($list as $line) {
$parts = [];
foreach ($line as $text) {
$parts[] = explode(' / ', $text);
}
$newArray[] = $parts;
}
var_dump($newArray);
/*
array (size=2)
0 =>
array (size=2)
0 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
1 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
1 =>
array (size=2)
0 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
1 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
*/
您正在创建一个新数组,而不是写入您正在展开的相同索引,您可以这样做:
$arr = [
0 => [
0 => '2020-11-01 / 2020-11-30',
1 => '2020-12-01 / 2020-12-31',
],
1 => [
0 => '2020-11-01 / 2020-11-30',
1 => '2020-12-01 / 2020-12-31',
]
];
foreach ($arr as $bulkIndex => $bulks) {
foreach ($bulks as $lineIndex => $line) {
$arr[$bulkIndex][$lineIndex] = explode(' / ', $line);
}
}
您可以通过嵌套 array_map
分解每个值中的数据来实现您想要的结果:
$data = array(
array(
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
),
array(
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
)
);
$result = array_map(function ($a) {
return array_map(function ($v) {
return explode(' / ', $v );
}, $a);
}, $data);
print_r($result);
我有多个数组,我正试图分解并划分其中的所有信息。出于某种原因,当我这样做时,我丢失了有关主数组的信息。
数组
[0] => Array (
[0] => 2020-11-01 / 2020-11-30
[1] => 2020-12-01 / 2020-12-31
)
[1] => Array (
[0] => 2020-11-01 / 2020-11-30
[1] => 2020-12-01 / 2020-12-31
)
我使用了以下代码:
foreach ($list as $line) {
foreach ($line as $text) {
$parts[] = explode(' / ', $text);
}
}
print_r($parts) 将 return 以下内容:
[0] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[1] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
[2] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[3] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
但不幸的是,这不是我要找的,我需要的是:
[0] => Array (
[0] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[1] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
)
[1] => Array (
[0] => Array (
[0] => 2020-11-01
[1] => 2020-11-30
)
[1] => Array (
[0] => 2020-12-01
[1] => 2020-12-31
)
)
我四处搜索,但没有找到一个简单的方法。希望有人能帮忙。谢谢!
如果您真的想保持原始阵列完好无损。一个解决方案是使用另一个数组将你的分解数组放在里面:
<?php
$list = [
[
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
],
[
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
]
];
$newArray = [];
foreach ($list as $line) {
$parts = [];
foreach ($line as $text) {
$parts[] = explode(' / ', $text);
}
$newArray[] = $parts;
}
var_dump($newArray);
/*
array (size=2)
0 =>
array (size=2)
0 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
1 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
1 =>
array (size=2)
0 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
1 =>
array (size=2)
0 => string '2020-11-01' (length=10)
1 => string '2020-11-30' (length=10)
*/
您正在创建一个新数组,而不是写入您正在展开的相同索引,您可以这样做:
$arr = [
0 => [
0 => '2020-11-01 / 2020-11-30',
1 => '2020-12-01 / 2020-12-31',
],
1 => [
0 => '2020-11-01 / 2020-11-30',
1 => '2020-12-01 / 2020-12-31',
]
];
foreach ($arr as $bulkIndex => $bulks) {
foreach ($bulks as $lineIndex => $line) {
$arr[$bulkIndex][$lineIndex] = explode(' / ', $line);
}
}
您可以通过嵌套 array_map
分解每个值中的数据来实现您想要的结果:
$data = array(
array(
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
),
array(
"2020-11-01 / 2020-11-30",
"2020-11-01 / 2020-11-30",
)
);
$result = array_map(function ($a) {
return array_map(function ($v) {
return explode(' / ', $v );
}, $a);
}, $data);
print_r($result);