Error reading position (3) of the Two-dimensional array: Invalid argument supplied for foreach

-2

How can I read the contents of array $detail[3] after using explode(implode) ?

I'm doing the following:

$aArray = array(
    "Titulo" => array(
        "Class|SubTitulo" => Array(
            "Detalhe01",
            "Detalhe02",
            "Detalhe03",
            "Atividades" => Array(
                "Atividade01;",
                "Atividade02;",
                "Atividade03;"
            )
        )
    )
);

foreach ($aArray as $title => $aInfo) {
    echo "Titulo: ".$title ."<br>";
    foreach ($aInfo as $subTitle => $aDetail) {
        $subTitle = explode("|", $subTitle);
        echo "-- Class: ".$subTitle[0] ."<br>";
        echo "-- SubTitulo: ".$subTitle[0] ."<br>";

        $detail = explode("|", implode("|", $aDetail));
        echo "----- Detalhe: " . $detail[0] ."<br>";
        echo "----- Detalhe: " . $detail[1] ."<br>";
        echo "----- Detalhe: " . $detail[2] ."<br>";
        foreach ($detail[3] as $activity => $value) { // O Erro da nesta linha
            echo "-- Atividade: ".$activity."<br>";
            echo "----- Valor: ".$value."<br>";
        }
    }
}

But it is giving invalid argument error

  

Warning: Invalid argument supplied for foreach ()

    
asked by anonymous 15.08.2015 / 21:36

2 answers

0

The problem is that your item key is not an index, replace:

$detail[3]

So:

$detail["Atividades"]
    
17.08.2015 / 02:56
0

To get the values of your two-dimensional array, do the following:

$content = [];

foreach ($aArray as $title => $aInfo) {
$content[]= "Titulo: ".$title;

    if (!empty($aInfo)) {
        foreach ($aInfo as $classAndSubTitle => $aDetail) {
            list($class, $subTitle) = explode("|", $classAndSubTitle);
            $content[]= "-- Class: " . $class;
            $content[]= "-- SubTitulo: " . $subTitle;
            $content[]= "----- Detalhe: " .  str_replace(';','',$aDetail[0]);
            $content[]= "----- Detalhe: " .  str_replace(';','',$aDetail[1]);
            $content[]= "----- Detalhe: " .  str_replace(';','',$aDetail[2]);
            if (!empty($aDetail['Atividades'])) {
                foreach ($aDetail['Atividades'] as $activity => $value) { 
                    $content[]= "-- Atividade: ".str_replace(';','',$activity);
                    $content[]= "----- Valor: ".str_replace(';','',$value);
                }
            }
        }
    }
}
echo implode("<br>", $content);
    
17.08.2015 / 20:19