Touring multidimensional array with foreach and its index.PHP

1

I have this code:

for($i = 1 ; $i<=$jornadas;$i++){
    $local = $equipos[rand(1,($jornadas-1))];
    $partido = array ("local"=>$local);
    $ronda[$i][] = $partido;
}

foreach($ronda as $a){
    echo "<p>Equipo Local ".$a['local'];
}

But when executing the code it puts me:

"Undefined index: local"

Would you know what the reason is?

    
asked by Borja Sanchez 17.06.2017 в 17:22
source

2 answers

2

I will answer this question even if it falls on deaf ears.

The problem is that an extra level is being created in the results array

 $jornadas=5;
    $equipos=["equipo1","equipo2","equipo3","equipo4","equipo5","equipo6","equipo7","equipo8"];
    for($i = 1 ; $i<=$jornadas;$i++){
        $local = $equipos[rand(1,($jornadas-1))];
        $partido = array ("local"=>$local);
        $ronda[$i][] = $partido;
               /*  ^
               /*  |------------------Aquí está el problema */
    }


    foreach($ronda as $a){
        echo "

    Equipo Local ".$a['local'];


    }

By doing this, the variable $ronda has this structure:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [local] => equipo5
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [local] => equipo4
                )
        )

    [3] => Array
        (
            [0] => Array
                (
                    [local] => equipo4
                )
        )

    [4] => Array
        (
            [0] => Array
                (
                    [local] => equipo3
                )
        )

    [5] => Array
        (
            [0] => Array
                (
                    [local] => equipo5
                )
        )
)

By iterating in this way in this data structure:

foreach($ronda as $a){
        echo "

    Equipo Local ".$a['local'];


    }

The variable $a contains this data:

Array
(
    [0] => Array
        (
            [local] => equipo4
        )

)

And when trying to access the $a['local'] index, it causes an error since that index is at a lower level.

With this code it works correctly:

$jornadas=5;
$equipos=["equipo1","equipo2","equipo3","equipo4","equipo5","equipo6","equipo7","equipo8"];
    for($i = 1 ; $i<=$jornadas;$i++){
        $local = $equipos[rand(1,($jornadas-1))];
        $partido = array ("local"=>$local);
        $ronda[$i] = $partido; //Línea que producía el error corregida

    }

    foreach($ronda as $a){

        echo "

Equipo Local ".$a['local'];


    }

Whose result is expected:

  

Local Team team3

     

Local Team team2

     

Local Team team2

     

Local Team team5

     

Local Team team3

    
answered by 03.04.2018 в 11:03
1
foreach ($ronda as $key => $a)
    echo $a;

In a foreach, an associative array indicates each of the values in the element behind the "as". If you want to print that value, you only need to show that value.

If you need to access the key, $ key contains 'local'.

    
answered by 17.06.2017 в 17:52