-2

I want help on this block of code

foreach ($summary as $key => $value) 
{
    if ($key === "Incs") {
        foreach ($value as $subkey => $subvalue) {
            foreach ($subvalue as $subsubkey => $subsubvalue) {
                echo "$subsubkey:\n";
                foreach ($subsubvalue as $subsubsubkey => $subsubsubvalue) {
                    echo $subsubsubvalue;
                }
            }
        }
    } else {
        echo "$key: $value\n";
    }
}

that is working by printing values of sub array echo $subsubsubvalue; but I want to use key to access each value like echo $subsubsubvalue[$subsubsubkey]; which seems to generate error. Is there anyway possible to achieve that?

foreach ($summary as $key => $value) 
{
    if ($key === "Incs") {
        foreach ($value as $subkey => $subvalue) {
            foreach ($subvalue as $subsubkey => $subsubvalue) {
                echo "$subsubkey:\n";
                foreach ($subsubvalue as $subsubsubkey => $subsubsubvalue) {
                    echo  $subsubsubvalue[$subsubsubkey];
                }
            }
        }
    } else {
        echo "$key: $value\n";
    }
}

Here is the array Wich I am trying print

array(8) { ["Eid"]=> string(6) "891551" ["Tr1"]=> string(1) "2" ["Tr2"]=> string(1) "0" ["Trh1"]=> string(1) "2" ["Trh2"]=> string(1) "0" ["Tr1OR"]=> string(1) "0" ["Tr2OR"]=> string(1) "0" ["Incs"]=> array(1) { [1]=> array(2) { [0]=> array(12) { ["Min"]=> int(24) ["Nm"]=> int(1) ["Aid"]=> string(5) "57092" ["ID"]=> string(5) "57092" ["Fn"]=> string(7) "Si-heon" ["Ln"]=> string(3) "Lee" ["Pnum"]=> int(0) ["Pn"]=> string(11) "Si-Heon Lee" ["PnumO"]=> int(0) ["IT"]=> int(36) ["Sc"]=> array(2) { [0]=> int(1) [1]=> int(0) } ["Sor"]=> int(3) } [1]=> array(9) { ["Min"]=> int(32) ["Nm"]=> int(1) ["ID"]=> string(7) "1067481" ["Pnum"]=> int(0) ["Pn"]=> string(13) "Dong-Ryul Lee" ["PnumO"]=> int(0) ["IT"]=> int(36) ["Sc"]=> array(2) { [0]=> int(2) [1]=> int(0) } ["Sor"]=> int(5) } } } }

I want to show them in table Time | Player | Incident

36'. Debryne. Goal 45'. Martial. Yellow card

5
  • 1
    What error do you get, exactly? Also, it's very difficult for us to comment on this if you don't show us some sample data, so we know what the code is trying to process. If you could show the output of var_export($summary); in your question that would help us a lot. See also How to Ask and how to produce a minimal reproducible example of your issue. You can edit your post. Thanks. Commented Apr 11, 2023 at 21:42
  • 2
    $subsubsubkey is a key of $subsubvalue, not $subsubsubvalue. So you could write echo $subsubvalue[$subsubsubkey];. Commented Apr 11, 2023 at 21:43
  • Why do you want to write it that way? Isn't $subsubsubvalue easier to write? Commented Apr 11, 2023 at 21:44
  • What you wrote would only work if the value of $subsubvalue[$subsubsubkey] were another array that has the same key as $subsubvalue. But in that case the first code wouldn't work because you can't echo arrays. Commented Apr 11, 2023 at 21:45
  • Instead of hammering with How can I access an array/object?, I've vote to close as a typo question. You already know to access array data in a loop -- you simply failed to do it in the innermost loop. Commented Apr 11, 2023 at 23:34

1 Answer 1

1

It seems like you're trying to access the value using the key when you're already looping through the values of the innermost array. To achieve what you want, you should just echo the key and value within the innermost loop directly. You don't need to use the key to access the value again, as you already have the value in $subsubsubvalue.

Also you may use $ssk, and $ssv as shorthand names. And as recomendation use functions for recursivity and call it after the first foreach

function rPrintNestedArray($array, $indent = 0) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            echo str_repeat(" ", $indent * 4) . "$key:\n";
            rPrintNestedArray($value, $indent + 1);
        } else {
            echo str_repeat(" ", $indent * 4) . "$key: $value\n";
        }
    }
}

Your code should look like.

    foreach ($summary as $key => $value) 
{
    if ($key === "Incs") {
        foreach ($value as $subkey => $subvalue) {
            foreach ($subvalue as $subsubkey => $subsubvalue) {
                echo "$subsubkey:\n";
                foreach ($subsubvalue as $subsubsubkey => $subsubsubvalue) {
                    echo "$subsubsubkey: $subsubsubvalue\n";
                }
            }
        }
    } else {
        echo "$key: $value\n";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.