Search code examples
phparrayslaravelforeachnested

How to add condition on nested array and add new value in laravel


I have data grouped by type and months name, i wanna make condition when data have 12 month or not. if it not so add the month.

this is the data that i wanna check:

array:1 [▼ // app\Http\Controllers\DashboardController.php:50
  "RM" => array:1 [▼
    "Februari" => array:2 [▼
      0 => array:42 [▶]
      1 => array:42 [▶]
    ]
  ]
]

im trying my best but i know it doesnt work at all. this is what im trying to do and it breaks the app

$months = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];
                $transaksi_grouped = $transaksi->groupBy([fn ($data) => $data->jenis_pagu->jenis, 'periode'])->toArray();

                foreach ($transaksi_grouped as $type => $row) {
                    foreach ($row as $month => $data) {
                        foreach ($months as $value) {
                            if ($month != $value) {
                                $new_value = [$value => [0]];
                                array_push($transaksi_grouped[$value], $new_value);
                            }
                        }
                    }
                }

i wanna check the data if it have month or not, if it not then add the month. this is what i expected:

array:1 [▼ // app\Http\Controllers\DashboardController.php:58
  "RM" => array:2 [▼
    "Januari" => []
    "Februari" => array:2 [▼
      0 => "data"
      1 => "data"
    ]
  ]
]

any helps is meaningfull for me, thanks in advance.


Solution

  • I believe I've managed to address the issue with the missing months in the transaction data. After reviewing the code, it seems that the solution now ensures that all types of transactions have data for each month of the year. By simulating transaction data and iterating through it, we've successfully filled in any gaps by adding empty arrays for the missing months. This approach ensures that the data is organized consistently, making it easier to analyze and work with.

    $transaksi = [
            (object)['jenis_pagu' => (object)['jenis' => 'test1'], 'periode' => 'Januari', 'data' => ['dado1', 'dado2']],
            (object)['jenis_pagu' => (object)['jenis' => 'test1'], 'periode' => 'Februari', 'data' => ['dado3']],
            (object)['jenis_pagu' => (object)['jenis' => 'test2'], 'periode' => 'Februari', 'data' => ['dado4']],
        ];
    
        $months = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];
    
        $transaksi_grouped = [];
    
        foreach ($transaksi as $data) {
            $jenis = $data->jenis_pagu->jenis;
            $periode = $data->periode;
            $data_value = $data->data ?? null;
    
            if (!isset($transaksi_grouped[$jenis])) {
                $transaksi_grouped[$jenis] = [];
            }
    
            if (!isset($transaksi_grouped[$jenis][$periode])) {
                $transaksi_grouped[$jenis][$periode] = [];
            }
    
            $transaksi_grouped[$jenis][$periode][] = $data_value;
        }
    
        foreach ($transaksi_grouped as &$type_data) {
            foreach ($months as $month) {
                if (!isset($type_data[$month])) {
                    $type_data[$month] = [];
                }
            }
        }
    
        unset($type_data);
        dd($transaksi_grouped);
    

    Result:

    enter image description here