Jump to content

Need help!


david212

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/145932-need-help/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/145932-need-help/#findComment-766129
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.