Page 1 of 1

string indexed array looping problem

Posted: Mon Sep 14, 2009 3:53 am
by lipun4u
Go through the following code..

Code: Select all

<?php
        /*function println($s) {
            echo "$s<BR>\n";
        }*/
        $countries = array ( "ca" => "Canada",
                             "cr" => "Costa Rica",
                             "de" => "Germany",
                             "uk" => "United Kingdom",
                             "us" => "United States"
                            );
        while(list($key,$val) = each($countries)) {
            echo "Element $key equals $val<BR>\n");
        }   
        
?>
It should print entire string indexed array. But why does it print the following ???

Code: Select all

3<BR>
2<BR>
0<BR>
9<BR>
4<BR>
6<BR>

Re: string indexed array looping problem

Posted: Mon Sep 14, 2009 4:04 am
by lord_webby

Code: Select all

<?php
 
$countries = array ( "ca" => "Canada",
                             "cr" => "Costa Rica",
                             "de" => "Germany",
                             "uk" => "United Kingdom",
                             "us" => "United States"
                            );
while(list($key,$val) = each($countries)) {
    echo "Element $key equals $val<BR>\n";
}   
        
?>
Outputs:

Element ca equals Canada
Element cr equals Costa Rica
Element de equals Germany
Element uk equals United Kingdom
Element us equals United States

Re: string indexed array looping problem

Posted: Mon Sep 14, 2009 4:33 am
by lipun4u
Though my code has some error, why the interpreter doesn't show these errors ??

Re: string indexed array looping problem

Posted: Mon Sep 14, 2009 4:49 am
by lord_webby

Code: Select all

<?php
 error_reporting(E_ALL); 
 ini_set("display_errors", 1); ?>
Include that on every page - you had an extra ")" at the end.

Re: string indexed array looping problem

Posted: Mon Sep 14, 2009 6:15 am
by lipun4u
thanx