2016-07-14 8 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

'ます$ this-> transformValue($値、$レベル+ 1);'何を返し、どのような変数に? –

+0

私は '$ level'が何のために使われているのか分かりません。 – Chay22

+0

@ Chay22 $レベルは配列の深さを決定するために使用され、関数は再帰的です。 – 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);