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
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]." | ";
    }
  }

?>

Link to comment
Share on other sites

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>";

?>


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.