Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

php - 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 ?

question from:https://stackoverflow.com/questions/65909160/array-issue-with-echoing-the-resutls

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...