david212 Posted February 19, 2009 Share Posted February 19, 2009 Hello! I have probelms with arrays but at this time with 2D arrays. I have coded this line: <?php $arx = array(array(1,2,3,4),array(5,6,7,8,9,10,11),array(12,13,14,15)); $s = sizeof($arx); for($i=0;$i<$s;$i++){ $s2 = sizeof($arx[$i]); for($j=0;$j<$s2;$j++){ if($arx[$i][$j]%2==0){ echo("<span style=\"color: red;\">".$arx[$i][$j]."</span><br>"); continue; } echo($arx[$i][$j]."<br>"); } } ?> and the result is 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 now i wanted to create a 2D arrays but using string, for example, <?php $arx = array(array("a","b","c","d"),array("e","f","g","h","i","j","k"),array("l","m","n","o")); $s = sizeof($arx); for($i=0;$i<$s;$i++){ $s2 = sizeof($arx[$i]); for($j=0;$j<$s2;$j++){ if($arx[$i][$j]%2==0){ echo("<span style=\"color: red;\">".$arx[$i][$j]."</span><br>"); continue; } echo($arx[$i][$j]."<br>"); } } ?> and the result is different, i want to put every second word in red, but all words appear in red.How can i solve this problem? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/145932-need-help/ Share on other sites More sharing options...
sklein99 Posted February 19, 2009 Share Posted February 19, 2009 You are trying to take the remainder of 'a'%2 and so forth, which doesn't make sense. One way to fix this is to just use a separate flag: color_red = false; for.... { for... { if (color_red) { show as red } else { show as black } color_red = !color_red; //toggle } } ... or if you love the mod operation, keep a separate running counter and mod that. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/145932-need-help/#findComment-766129 Share on other sites More sharing options...
sasa Posted February 19, 2009 Share Posted February 19, 2009 try if(ord($arx[$i][$j])%2==0) ... Quote Link to comment https://forums.phpfreaks.com/topic/145932-need-help/#findComment-766132 Share on other sites More sharing options...
david212 Posted February 19, 2009 Author Share Posted February 19, 2009 Thank, it worked! Quote Link to comment https://forums.phpfreaks.com/topic/145932-need-help/#findComment-766155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.