2016-07-14 88 views
-2

我的下一個問題,我有這樣的多維數組:PHP - 問題與函數的返回和多維數組

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不保存函數返回值。我的錯誤在哪裏?

+0

'$這 - > transformValue($值,$水平+ 1):

而這種輸出通過將以下代碼返回? –

+0

我沒有得到'$ level'用於什麼。 – Chay22

+0

@ Chay22 $ level用於確定數組的深度,該函數是遞歸的。 – Ale

回答

1

你應該從這個呼叫分配值:

$this->transformValue($value, $level + 1);

給一個變量。像:

$return[$key] = $this->transformValue($value, $level + 1);

每次這個函數被遞歸調用,該return聲明將數據發回&沒有什麼可認爲返回的數據。

SOLUTION

你可以改變你的函數如下&實現無遞歸相同的輸出:

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>'; 
+0

謝謝,但問題不在於函數。如果在返回函數之前調用print_r($ return),則正確顯示值,但變量$ return_value_function不包含函數返回的值 – Ale

+0

發佈更新後的解決方案。 – tovishalck

+0

謝謝,因爲這是我的解決方案! – Ale

0

請參考下面的代碼。它會根據你的意願返回數組。

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);