Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:46:37+00:00 2026-06-15T05:46:37+00:00

I have an associative array in which every single value contains one single line

  • 0

I have an associative array in which every single value contains one single line of code. I display them in the foreach() loop. How can I highlight all of them?

I’ve found the highlight_string() function. It does work, yes, but only with PHP tags surrounding the code. The problem is that I don’t want to display these tags. It’s an array, so the tags should be added to every single value which would make the outcome looks ugly.

Adding tags to the beginning and the end of the array doesn’t work either – I won’t use highlight_string() on the whole array (surrounging foreach() loop).

And if I get rid of the PHP tags, highlighting stops working.

Is there any way to do this without including 3rd party applications?

EXAMPLE:

This is the array:

$var = array(0 => 'for($i=0;$i<5;$i++)', 1 => '// do something', 2 => '$i++', 3 => '}');

This is how I display lines of code one after another:

foreach($var as $line) 
{
   echo $line . '<br>';
}

And I want to display them as the highlighted code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:46:38+00:00Added an answer on June 15, 2026 at 5:46 am

    This will hopefully be of assistance here:

    $code_array = array(
        '<?php',
        'echo "Hello";',
        '?>'
    );
    
    echo highlight_string(implode("\r\n", $code_array), TRUE);
    

    Addition: Since you added your code sample this will work too 🙂

    $var = array(0 => 'for($i=0;$i<5;$i++)', 1 => '// do something', 2 => '$i++', 3 => '}');
    $var = array_merge(array('<?php'), $var, array('?>'));
    echo highlight_string(implode("\r\n", $var), TRUE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php loop through associative arrays i have this piece of code which
I have a function which must return one dimensional associate array, like $user_info[$index]=value where
I have an array of data which contains associative array rows and I would
I got a phptal template question, I have an associative array which contains HTML
I have an associative array which when var dumped looks like this: Array (
I have an indexed array which I've generated from an associative array with this
Possible Duplicate: php loop through associative arrays i have a variable which prints an
I have an associative array that I want to display using TileList. However, it
I have an array which may contain numeric or associative keys, or both: $x
I have 'multidimensional associative' javascript array (which in fact is object with properties as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.