mardi 14 juin 2016

laravel iterate multidemensional arrays of depth 4

Four models: Animal, Vertebrate, Mamal, WarmBlooded

Laravel Model Relationships: one animal has many vertebrates and one vertebrates has many mamals and one mamal has many warmblooded.

public function test() {

    $animals[0] = 'A';
    $vertebrates[0][0] = 'AA';
    $vertebrates[0][1] = 'BB';
    $mamals[0][0][0] = 'AAA';
    $mamals[0][1][0] = 'AAB';
    $mamals[0][2][0] = 'ABA';
    $mamals[0][2][1] = 'BAA';
    $warmblooded[0][0][0][0] = 'AAAA';
    $warmblooded[0][1][0][0] = 'BBBB';
    $warmblooded[0][2][0][0] = 'CCCC';
    $warmblooded[0][2][1][1] = 'DDDD';

    foreach ($animals as $animal => $value1) {
        var_dump($animals[$animal]);
        foreach ($vertebrates as $vertebrate => $value2) {
            if (!empty($vertebrates[$animal][$vertebrate])) {
                var_dump($vertebrates[$animal][$vertebrate]);
            }
            foreach ($mamals as $mamal => $value3) {
                if (!empty($mamals[$animal][$vertebrate][$mamal])) {
                    var_dump($mamals[$animal][$vertebrate][$mamal]);
                }
                foreach ($warmblooded as $warmblood => $value4) {
                    if (!empty($warmblooded[$animal][$vertebrate][$mamal][$warmblood])) {
                        var_dump($warmblooded[$animal][$vertebrate][$mamal][$warmblood]);
                    }
                }
            }
        }
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire