Jump to content

Highlighting a value in an array?


Solarpitch

Recommended Posts

Hey Guys,

 

I have the below piece of code which will basically print to screen 16 random numbers from a value of 0 - 9. Where you see...

 

$value[2] = $num1;

$value[6] = $num2;

$value[8] = $num3;

$value[10] = $num4;

 

.. I am manipulating those array keys to insert my own 4 numbers into that array. What I am looking to do is when the 16 numbers are printed to screen, I want to highlight the above values on the screen. So I want to make the array values [2], [6], [8], [10] bold on the screen. Just wondering if this can be done. Here's the script

 

 

<?php

for ($i=0; $i<16; $i++) {	
$value[$i] = random_num(1);
}

$value[2] = $num1;
$value[6] = $num2;
$value[8] = $num3;
$value[10] = $num4;

echo "<strong Random Set:</strong>   ";
for ($i=0; $i<16; $i++) {

        echo $value[$i]." | ";
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/93487-highlighting-a-value-in-an-array/
Share on other sites

<?php

  for ($i=0; $i<16; $i++) {	
    $value[$i] = random_num(1);
  }

  $value[2] = $num1;
  $value[6] = $num2;
  $value[8] = $num3;
  $value[10] = $num4;

  echo "<strong Random Set:</strong>   ";
  for ($i=0; $i<16; $i++) {
    if ($i % 2) {
      echo '<b>' . $value[$i]."</b> | ";
    } else {
      echo $value[$i]." | ";
    }
  }

?>

Ah I see,

 

that makes every second value in the array bold. But I want to pick out only the 4 keys I listed. So would you modify it to something like...

 

<?php

  for ($i=0; $i<16; $i++) {	
    $value[$i] = random_num(1);
  }

  $value[2] = $num1;
  $value[6] = $num2;
  $value[8] = $num3;
  $value[10] = $num4;

  echo "<strong Random Set:</strong>   ";
  for ($i=0; $i<16; $i++) {
    if ($value[$i] == 2) {     // <--------------------------
      echo '<b>' . $value[$i]."</b> | ";
    } else {
      echo $value[$i]." | ";
    }
  }
echo "<br><br>";

?>


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.