Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

faizalanwar's avatar

looping multidimentional array on pivot table laravel

Excuse me, I want to ask about the iteration step.

i have productdata like this

productdata

and criteria data

criteriadata

and alternative data as a liaison / place for the main data from the two tables above

alternative

i want to make iteration or loop $nilaiprefensi like below

enter image description here

with calculations like this

$nilaiprefensi[0]= [["A1,A2"],[2-4],[5-9],[10-9],[3-9],[6-8],[4-8],[120-120]]
$nilaiprefensi[1]= [["A1,A3"],[2-3],[5-8],[10-6],[3-8],[6-16],[4-9],[120-155]]
..
$nilaiprefensi[5]= [["A2,A1"],[4-2],[9-5],[9-10],[9-3],[8-6],[8-4],[120,120]]

I've tried several times to make this loop function, but it doesn't work, here's my latest code

       for ($i = 0; $i <$loop; $i++) { //30
            for ($j = 0; $j < count($products); $j++) {
                if ($i !== $j) {
                    $p[$i][$j][0] = ('A' . $i + 1) . ' , A' . $j + 1;
                    for ($k = 1; $k < count($kriteria); $k++) {
                            $p[$i][$j][$k]=$alternatif[$k-1]->kriteria_data."-".$alternatif[$i]->kriteria_data;
                    }
                }
            }
        }
        dd($p);

What is the correct looping function to get the expected result, please help, thanks 🙏🙏

0 likes
0 replies

Please or to participate in this conversation.