maxbld Posted March 8, 2013 Share Posted March 8, 2013 Dear all, 10 years ago I was a skilled PHP programmer... Now after many years of inactivity I'm facing a newbye problem I can't figure out. I've written this piece of code: $found = array('uno','due', 'tre'); for ($i=0;$i<count($found);$i++){ echo $found[i]; } my output page is empty?? Shouldn't I see unoduetre? I mean I see in the debugger the array is fed and the loop is done!! Please be kind help me out, I feel like daydreaming... BR, Max. Link to comment https://forums.phpfreaks.com/topic/275405-array-help/ Share on other sites More sharing options...
AyKay47 Posted March 8, 2013 Share Posted March 8, 2013 $found should read $found[$i] Note the addition of the variable denotation symbol $ before i As a side note, the use of a foreach loop to iterate through an array gives you access to both the keys and values of an array. Link to comment https://forums.phpfreaks.com/topic/275405-array-help/#findComment-1417487 Share on other sites More sharing options...
jazzman1 Posted March 8, 2013 Share Posted March 8, 2013 Php is not C Link to comment https://forums.phpfreaks.com/topic/275405-array-help/#findComment-1417488 Share on other sites More sharing options...
Barand Posted March 8, 2013 Share Posted March 8, 2013 also for ($i=0, $k =count($found); $i<$k; $i++) is more efficient - it only counts the array once Link to comment https://forums.phpfreaks.com/topic/275405-array-help/#findComment-1417490 Share on other sites More sharing options...
salathe Posted March 8, 2013 Share Posted March 8, 2013 for ($i=0, $k =count($found); $i<$k; $i++)is more efficient - it only counts the array once And foreach is an array's best friend. Link to comment https://forums.phpfreaks.com/topic/275405-array-help/#findComment-1417495 Share on other sites More sharing options...
maxbld Posted March 8, 2013 Author Share Posted March 8, 2013 Right, right, right!! Sorry for silly question! Link to comment https://forums.phpfreaks.com/topic/275405-array-help/#findComment-1417501 Share on other sites More sharing options...
salathe Posted March 8, 2013 Share Posted March 8, 2013 It's not silly at all. Having this sorts of questions on the intertubes is invaluable to others suffering from similar mistakes. Link to comment https://forums.phpfreaks.com/topic/275405-array-help/#findComment-1417503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.