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 6129445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:44:36+00:00 2026-05-23T16:44:36+00:00

I am working with PHP, and I want to display some data I have

  • 0

I am working with PHP, and I want to display some data I have in an array (from a mysql db). The problem is that the first column not always have the same length, so while some rows containing shorter strings in the first column are all well aligned, in those with larger strings the second column gets aligned more to the right. The array looks like this:

$array[0][0] = "string0gvmdksd";
$array[0][1] = "42134";
$array[1][0] = "string1fges";
$array[1][1] = "234";
$array[2][0] = "string2gvmdksd sgdfsgdfg gffgdfg";
$array[2][1] = "87342";
$array[3][0] = "string3gffd";
$array[3][1] = "34";

I though about a solution. Getting the length of the longer string in the array[$i][0], then setting a variable with X number of spaces and a tab at the end (X is the longer string length), but that doesn’t seem to work.

Here’s my code:

function addTabs($input) {
    $length = 0;
    foreach($input as $entry) {
        if(strlen($entry[0])>$length) $length = strlen($entry[0]);
    }

    return str_repeat(" ", $length)."\t";
}

$data = "";
$tabs = addTabs( $array );
foreach( $array as $entry ) {
    $data .=  $entry[0] . $tabs . $entry[1] . "\n";
}

echo "<pre>$data</pre>";

But it displays:

string0gvmdksd                                  42134
string1fges                                 234
string2gvmdksd sgdfsgdfg gffgdfg                                    87342
string3gffd                                 34

Anyone has a solution for this?

  • 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-05-23T16:44:37+00:00Added an answer on May 23, 2026 at 4:44 pm

    If you really have to use <pre> tag then its best to use sprintf function like this:

    function maxLen($input) {
        $length = 0;
        foreach($input as $entry)
            if(strlen($entry[0])>$length) $length = strlen($entry[0]);
        return $length;
    }
    $len = maxLen($array);
    foreach( $array as $entry ) {
        $data .=  sprintf("%-$len"."s\t%s\n", $entry[0], $entry[1]);
    }
    echo "<pre>$data</pre>";
    

    OUTPUT

    string0gvmdksd                      42134
    string1fges                         234
    string2gvmdksd sgdfsgdfg gffgdfg    87342
    string3gffd                         34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI GEEKS I am working in PHP and Mysql. I have an array from
I am working with php and want to have a page that has a
I am working on a website that displays some data from DB that changes
I have a site that displays data on travel products from a MySQL database.
I'm working on Ubuntu.(Linux) I want to display all .php pages to .html in
Iam working on Ubuntu.(Linux) I want to redirect from the page one.php to two.php,
Later this year I want to release a PHP framework that I've been working
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
Im working on a website (html,css,javascript, ajax, php,mysql), and I want to restrict the
I am working on a site that needs to display inventory data. I am

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.