-
How can i print values of array in this program? it is printing the key?
about 8 years ago
-
almost 8 years ago
No matter which loop you use, you just need proper format to get key/value<?php $CHAMP = array("ESP"=>"BARCELONA", "GER"=>"BAYERN", "POR"=>"BENFICA","ENG"=>"CHELSIA"); $TEAM = array("ESP"=>"ATLETICO", "GER"=>"LEVERKUSEN", "POR"=>"PORTO","ENG"=>"ARSENAL","ESP"=>"REAL MADRID"); $keys = array_keys($TEAM); $TEST = array("ESP"=>"BARCELONA", "GER"=>"BAYERN","ENG"=>"ARSENAL","ESP"=>"REAL MADRID"); while (list($key, $val) = each($CHAMP)) { print "While loop: $key is $val\n"; } for ($i = 0; $i < count($TEAM); ++$i) { print "For loop: ".$keys[$i]." is ".$TEAM[$keys[$i]]."\n"; } foreach($TEST as $key => $val) { print "Foreach loop: $key is $val\n"; } ?>
-
-
about 8 years ago
Hello Basant,
I just go through your code and i found that you are retrieving keys using array_key function and using for loop then. you need to use the foreach loop to retrieve the both keys as well as values. Kindly check the code below.
<?php $arr = array('name'=>'Deepak','designation'=>'Software Engineer'); foreach($arr as $key=>$val){ echo $key. "\t" . $val; } ?>
Thank You
Deepak Verma
-
2 Answer(s)