我有下一个问题,我有这个多维数组:
Array
(
[0] => Array
(
[0] => Array
(
[start_date] => 2016-07-14 07:00
[end_date] => 2016-07-14 07:20
[type] => 2
)
[1] => Array
(
[start_date] => 2016-07-14 07:20
[end_date] => 2016-07-14 07:40
[type] => 2
)
[2] => Array
(
[start_date] => 2016-07-14 07:40
[end_date] => 2016-07-14 08:00
[type] => 2
)
[3] => Array
(
[start_date] => 2016-07-14 08:00
[end_date] => 2016-07-14 08:15
[type] => 3
)
[4] => Array
(
[start_date] => 2016-07-14 08:15
[end_date] => 2016-07-14 08:30
[type] => 3
)
)
[1] => Array
(
[0] => Array
(
[start_date] => 2016-07-15 07:00
[end_date] => 2016-07-15 07:20
[type] => 2
)
[1] => Array
(
[start_date] => 2016-07-15 07:20
[end_date] => 2016-07-15 07:40
[type] => 2
)
[2] => Array
(
[start_date] => 2016-07-15 07:40
[end_date] => 2016-07-15 08:00
[type] => 2
)
[3] => Array
(
[start_date] => 2016-07-15 08:00
[end_date] => 2016-07-15 08:15
[type] => 3
)
[4] => Array
(
[start_date] => 2016-07-15 08:15
[end_date] => 2016-07-15 08:30
[type] => 3
)
)
[2] => Array
(
[0] => Array
(
[start_date] => 2016-07-18 07:00
[end_date] => 2016-07-18 07:20
[type] => 2
)
[1] => Array
(
[start_date] => 2016-07-18 07:20
[end_date] => 2016-07-18 07:40
[type] => 2
)
[2] => Array
(
[start_date] => 2016-07-18 07:40
[end_date] => 2016-07-18 08:00
[type] => 2
)
[3] => Array
(
[start_date] => 2016-07-18 08:00
[end_date] => 2016-07-18 08:15
[type] => 3
)
[4] => Array
(
[start_date] => 2016-07-18 08:15
[end_date] => 2016-07-18 08:30
[type] => 3
)
)
[3] => Array
(
[0] => Array
(
[start_date] => 2016-07-19 07:00
[end_date] => 2016-07-19 07:20
[type] => 2
)
[1] => Array
(
[start_date] => 2016-07-19 07:20
[end_date] => 2016-07-19 07:40
[type] => 2
)
[2] => Array
(
[start_date] => 2016-07-19 07:40
[end_date] => 2016-07-19 08:00
[type] => 2
)
[3] => Array
(
[start_date] => 2016-07-19 08:00
[end_date] => 2016-07-19 08:15
[type] => 3
)
[4] => Array
(
[start_date] => 2016-07-19 08:15
[end_date] => 2016-07-19 08:30
[type] => 3
)
)
[4] => Array
(
[0] => Array
(
[start_date] => 2016-07-20 07:00
[end_date] => 2016-07-20 07:20
[type] => 2
)
[1] => Array
(
[start_date] => 2016-07-20 07:20
[end_date] => 2016-07-20 07:40
[type] => 2
)
[2] => Array
(
[start_date] => 2016-07-20 07:40
[end_date] => 2016-07-20 08:00
[type] => 2
)
[3] => Array
(
[start_date] => 2016-07-20 08:00
[end_date] => 2016-07-20 08:15
[type] => 3
)
[4] => Array
(
[start_date] => 2016-07-20 08:15
[end_date] => 2016-07-20 08:30
[type] => 3
)
)
[5] => Array
(
[0] => Array
(
[start_date] => 2016-07-21 07:00
[end_date] => 2016-07-21 07:20
[type] => 2
)
[1] => Array
(
[start_date] => 2016-07-21 07:20
[end_date] => 2016-07-21 07:40
[type] => 2
)
[2] => Array
(
[start_date] => 2016-07-21 07:40
[end_date] => 2016-07-21 08:00
[type] => 2
)
[3] => Array
(
[start_date] => 2016-07-21 08:00
[end_date] => 2016-07-21 08:15
[type] => 3
)
[4] => Array
(
[start_date] => 2016-07-21 08:15
[end_date] => 2016-07-21 08:30
[type] => 3
)
)
[6] => Array
(
[0] => Array
(
[start_date] => 2016-07-22 07:00
[end_date] => 2016-07-22 07:20
[type] => 2
)
[1] => Array
(
[start_date] => 2016-07-22 07:20
[end_date] => 2016-07-22 07:40
[type] => 2
)
[2] => Array
(
[start_date] => 2016-07-22 07:40
[end_date] => 2016-07-22 08:00
[type] => 2
)
[3] => Array
(
[start_date] => 2016-07-22 08:00
[end_date] => 2016-07-22 08:15
[type] => 3
)
[4] => Array
(
[start_date] => 2016-07-22 08:15
[end_date] => 2016-07-22 08:30
[type] => 3
)
)
)
此外,我还有以下功能:
public function transformValue($array = [], $level = 1) {
$return = [];
foreach($array as $key => $value){
if(is_array($value)){
$this->transformValue($value, $level + 1);
} else{
$return[$key] = $value;
}
}
return $return;
}
此函数接收此多数组作为参数,因为我需要按如下方式对其进行规范化:
Array
(
[start_date] => 2016-07-14 07:00
[end_date] => 2016-07-14 07:20
[type] => 2
)
Array
(
[start_date] => 2016-07-14 07:20
[end_date] => 2016-07-14 07:40
[type] => 2
)
Array
(
[start_date] => 2016-07-14 07:40
[end_date] => 2016-07-14 08:00
[type] => 2
)
Array
(
[start_date] => 2016-07-14 08:00
[end_date] => 2016-07-14 08:15
[type] => 3
)
Array
(
[start_date] => 2016-07-14 08:15
[end_date] => 2016-07-14 08:30
[type] => 3
)
Array
(
[start_date] => 2016-07-15 07:00
[end_date] => 2016-07-15 07:20
[type] => 2
)
Array
(
[start_date] => 2016-07-15 07:20
[end_date] => 2016-07-15 07:40
[type] => 2
)
Array
(
[start_date] => 2016-07-15 07:40
[end_date] => 2016-07-15 08:00
[type] => 2
)
Array
(
[start_date] => 2016-07-15 08:00
[end_date] => 2016-07-15 08:15
[type] => 3
)
Array
(
[start_date] => 2016-07-15 08:15
[end_date] => 2016-07-15 08:30
[type] => 3
)
Array
(
[start_date] => 2016-07-18 07:00
[end_date] => 2016-07-18 07:20
[type] => 2
)
Array
(
[start_date] => 2016-07-18 07:20
[end_date] => 2016-07-18 07:40
[type] => 2
)
Array
(
[start_date] => 2016-07-18 07:40
[end_date] => 2016-07-18 08:00
[type] => 2
)
Array
(
[start_date] => 2016-07-18 08:00
[end_date] => 2016-07-18 08:15
[type] => 3
)
Array
(
[start_date] => 2016-07-18 08:15
[end_date] => 2016-07-18 08:30
[type] => 3
)
Array
(
[start_date] => 2016-07-19 07:00
[end_date] => 2016-07-19 07:20
[type] => 2
)
Array
(
[start_date] => 2016-07-19 07:20
[end_date] => 2016-07-19 07:40
[type] => 2
)
Array
(
[start_date] => 2016-07-19 07:40
[end_date] => 2016-07-19 08:00
[type] => 2
)
Array
(
[start_date] => 2016-07-19 08:00
[end_date] => 2016-07-19 08:15
[type] => 3
)
Array
(
[start_date] => 2016-07-19 08:15
[end_date] => 2016-07-19 08:30
[type] => 3
)
Array
(
[start_date] => 2016-07-20 07:00
[end_date] => 2016-07-20 07:20
[type] => 2
)
Array
(
[start_date] => 2016-07-20 07:20
[end_date] => 2016-07-20 07:40
[type] => 2
)
Array
(
[start_date] => 2016-07-20 07:40
[end_date] => 2016-07-20 08:00
[type] => 2
)
Array
(
[start_date] => 2016-07-20 08:00
[end_date] => 2016-07-20 08:15
[type] => 3
)
Array
(
[start_date] => 2016-07-20 08:15
[end_date] => 2016-07-20 08:30
[type] => 3
)
Array
(
[start_date] => 2016-07-21 07:00
[end_date] => 2016-07-21 07:20
[type] => 2
)
Array
(
[start_date] => 2016-07-21 07:20
[end_date] => 2016-07-21 07:40
[type] => 2
)
Array
(
[start_date] => 2016-07-21 07:40
[end_date] => 2016-07-21 08:00
[type] => 2
)
Array
(
[start_date] => 2016-07-21 08:00
[end_date] => 2016-07-21 08:15
[type] => 3
)
Array
(
[start_date] => 2016-07-21 08:15
[end_date] => 2016-07-21 08:30
[type] => 3
)
Array
(
[start_date] => 2016-07-22 07:00
[end_date] => 2016-07-22 07:20
[type] => 2
)
Array
(
[start_date] => 2016-07-22 07:20
[end_date] => 2016-07-22 07:40
[type] => 2
)
Array
(
[start_date] => 2016-07-22 07:40
[end_date] => 2016-07-22 08:00
[type] => 2
)
Array
(
[start_date] => 2016-07-22 08:00
[end_date] => 2016-07-22 08:15
[type] => 3
)
Array
(
[start_date] => 2016-07-22 08:15
[end_date] => 2016-07-22 08:30
[type] => 3
)
我的问题:当我调用我的函数并分配给一个变量时,该函数似乎没有为变量分配任何值,但是如果我使用 print_r() 检查函数的返回,则会根据需要显示值。我应该跳过是事情,我需要"推动"才能继续。正是以这种方式声明了带有函数返回的变量:
$return_value_function = [];
$return_value_function = $this->transformValue($array_values);
变量 $return_value_function 不会保存函数返回值。我的错误在哪里?
您应该从此调用中分配值:
$this->transformValue($value, $level + 1);
到变量。喜欢:
$return[$key] = $this->transformValue($value, $level + 1);
每次递归调用此函数时,return
语句都会将数据发回,并且没有任何东西可以保存返回的数据。
溶液
您可以按如下方式更改函数并实现相同的输出而无需递归:
function transformValue($array = []) {
$return = [];
foreach($array as $key => $value) {
if(is_array($value)) {
$return = array_merge( $return, $value );
}
}
return $return;
}
输出为:
Array
(
[0] => Array
(
[start_date] => 01 Jul, 2016
[type] => 1
)
[1] => Array
(
[start_date] => 02 Jul, 2016
[type] => 1
)
[2] => Array
(
[start_date] => 03 Jul, 2016
[type] => 1
)
[3] => Array
(
[start_date] => 04 Jul, 2016
[type] => 1
)
[4] => Array
(
[start_date] => 05 Jul, 2016
[type] => 1
)
[5] => Array
(
[start_date] => 06 Jul, 2016
[type] => 2
)
[6] => Array
(
[start_date] => 07 Jul, 2016
[type] => 2
)
[7] => Array
(
[start_date] => 08 Jul, 2016
[type] => 2
)
[8] => Array
(
[start_date] => 09 Jul, 2016
[type] => 2
)
[9] => Array
(
[start_date] => 10 Jul, 2016
[type] => 2
)
[10] => Array
(
[start_date] => 11 Jul, 2016
[type] => 3
)
[11] => Array
(
[start_date] => 12 Jul, 2016
[type] => 3
)
[12] => Array
(
[start_date] => 13 Jul, 2016
[type] => 3
)
[13] => Array
(
[start_date] => 14 Jul, 2016
[type] => 3
)
[14] => Array
(
[start_date] => 15 Jul, 2016
[type] => 3
)
[15] => Array
(
[start_date] => 16 Jul, 2016
[type] => 4
)
[16] => Array
(
[start_date] => 17 Jul, 2016
[type] => 4
)
[17] => Array
(
[start_date] => 18 Jul, 2016
[type] => 4
)
[18] => Array
(
[start_date] => 19 Jul, 2016
[type] => 4
)
[19] => Array
(
[start_date] => 20 Jul, 2016
[type] => 4
)
[20] => Array
(
[start_date] => 21 Jul, 2016
[type] => 5
)
[21] => Array
(
[start_date] => 22 Jul, 2016
[type] => 5
)
[22] => Array
(
[start_date] => 23 Jul, 2016
[type] => 5
)
[23] => Array
(
[start_date] => 24 Jul, 2016
[type] => 5
)
[24] => Array
(
[start_date] => 25 Jul, 2016
[type] => 5
)
)
请注意,我的示例数组仅包含这 2 个键。
并且此输出通过以下代码返回:
$return_value_function = [];
$return_value_function = transformValue($arr);
echo '<pre>';print_r($return_value_function);echo '</pre>';
请参考下面的代码。它将根据需要返回数组。
public function transformValue($array) {
foreach($array as $subarray){
foreach($subarray as $value){
$finalArray[] = $value;
}
}
return $finalArray;
}
$finalArray= $this->transformValue($array_values);
echo "<pre>";
print_r($finalArray);