简体   繁体   中英

Iterate through an array using foreach in php

Does the following code work in php4

foreach ($array as $i => $values) {
    print "$i {\n";
    foreach ($values as $key => $value) {
        print "    $key => $value\n";
    }
    print "}\n";
}

This works with php5 but the same loop with no changes does not work with the version 4. The foreach iterates through the loop but the values are not displayed. Could someone help me with this

Should work fine in PHP 4 there is nothing PHP 5 specific there.

It is more likely your array is different in between the 2 versions. As we can't see the entire code its difficult to tell.

Should work in php4 as well:

foreach ($array as $i => $values) {
    echo $i."{\n";
    foreach ($values as $key => $value) {
        echo $key ."=>". $value."\n";
    }
    echo "}\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM