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

Asked

Viewed 142 times

-2

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

I’m doing it this way:

$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 is giving invalid argument error

Warning: Invalid argument supplied for foreach()

  • Friend, have you ever thought of making an object? arrays so leaves your system very slow and with little performance

  • When you give a print_r($aArray); how it comes to you?

  • @Andrébaill using print_r the array is displayed normally. Array ( [Titulo] => Array ( [Class|SubTitulo] => Array ( [0] => Detalhe01 [1] => Detalhe02 [2] => Detalhe03 [Atividades] => Array ( [0] => Atividade01; [1] => Atividade02; [2] => Atividade03; ) ) ) )

  • @Israelzebulon believe that for what I’m doing, objects would complicate a lot, but you could give an example of how it would look ?

  • 1

    A curiosity, what is the use of this strange line: $detail = explode("|", implode("|", $aDetail));? If it’s just to take the keys out of the array, PHP already has a function ready for it: $detail = array_values( $aDetail ). But I believe that even this is not necessary, with key => value.

2 answers

0


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

$detail[3]

That’s why:

$detail["Atividades"]
  • Thank you, I managed to solve the problem by doing the following foreach ($aDetail[Atividades] as $activity => $value) because if I try to do like you said $detail['Atividades'] still of error because of the explode(implode)

  • In my example there is no error.

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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.