Markos
Markos

Reputation: 208

Array issue with echoing the resutls

This is my parsed array. I parse some elements and placed them into my array $results. I think i am somewhere wrong cause i want to echo everything speacially the array [display] but i can't

Array
(
[Forologiki_Periodos] => 1ος Μήνας 2020
[Hmerologiaki_periodos] => 01/01/2020 - 31/01/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/01/2020
[Hmerologiaki_periodos_ews] => 31/01/2020
[display] => Array
    (
        [0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
        [1] => vatF2
        [2] => 2020
        [3] => oneMonth
        [4] => 01/01/2020
        [5] => 31/01/2020
        [6] => 01/01/2020
        [7] => 31/01/2020
    )

)
Array
(
[Forologiki_Periodos] => 2ος Μήνας 2020
[Hmerologiaki_periodos] => 01/02/2020 - 29/02/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/02/2020
[Hmerologiaki_periodos_ews] => 29/02/2020
[display] => Array
    (
        [0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
        [1] => vatF2
        [2] => 2020
        [3] => oneMonth
        [4] => 01/02/2020
        [5] => 29/02/2020
        [6] => 01/02/2020
        [7] => 29/02/2020
    )

)
Array
(
[Forologiki_Periodos] => 3ος Μήνας 2020
[Hmerologiaki_periodos] => 01/03/2020 - 31/03/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/03/2020
[Hmerologiaki_periodos_ews] => 31/03/2020
[display] => Array
    (
        [0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
        [1] => vatF2
        [2] => 2020
        [3] => oneMonth
        [4] => 01/03/2020
        [5] => 31/03/2020
        [6] => 01/03/2020
        [7] => 31/03/2020
    )

) 

foreach ($result as $value){
    echo $value;
}

With this foreach i echo only the last element of $value, it should be echo all the 12 values. Also how can i get into the array [display] and echo values ?

Upvotes: 1

Views: 52

Answers (2)

spzout
spzout

Reputation: 104

You can loop directy on the display as

foreach ($result['display'] as $display) {
    echo $display . '<br>';
}

But you can imbricate 2 loops

foreach ($result as $value) {
    if (is_array($value)) {
        foreach ($value as $display) {
            echo '----' . $display . '<br>';
        }
    } else {
        echo $value . '<br>';
    }
}

The best solution is to use a function:

function displayArray($array)
{
    foreach ($array as $each) {
        if (is_array($each)) {
            displayArray($each);
        }

        echo $each . '<br>';
    }
}

So you can call the function like this:

displayArray($result);

Upvotes: 1

Asad Nur
Asad Nur

Reputation: 31

Pls try this:

foreach (@$result['display'] as $key=>$value){
    echo $value;
}

Upvotes: 0

Related Questions