-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
– Israel Zebulon
When you give a print_r($aArray); how it comes to you?
– Sr. André Baill
@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; ) ) ) )
– Roberto Valentim
@Israelzebulon believe that for what I’m doing, objects would complicate a lot, but you could give an example of how it would look ?
– Roberto Valentim
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.– Bacco