Home > Backend Development > PHP Problem > How to align php arrays

How to align php arrays

PHPz
Release: 2023-04-20 15:00:35
Original
795 people have browsed it

In PHP, arrays are a very common data type, and for array typesetting, sometimes we need to perform alignment operations. Below I will introduce how to align PHP arrays from several aspects.

  1. Use printf or sprintf function

Use printf or sprintf function to control the output format. Both functions have the same parameter, which is the format string. Use the % placeholder in the format string to represent the variables to be output next, and then use commas to separate the actual parameters, so that the output can be aligned.

For example:

<?php
$items = array(
    array(&#39;name&#39; => 'apple', 'price' => 10, 'count' => 3),
    array('name' => 'banana', 'price' => 15, 'count' => 2),
    array('name' => 'orange', 'price' => 8, 'count' => 4)
);

foreach($items as $item){
    printf("%-10s%5d%5d\n", $item['name'], $item['price'], $item['count']);
}
?>
Copy after login

The above code will output:

apple        10    3
banana       15    2
orange        8    4
Copy after login

Among them, the 10 in s represents the length of the output string, and the - sign represents left alignment; ] represents If the length of the output number is less than 5 digits, a space will be added in front of it. If it exceeds 5 digits, the actual number of digits will be output.

  1. Use str_pad function

PHP’s built-in str_pad function can also implement alignment operations. The str_pad function has three parameters. The first parameter represents the string that needs to be aligned, the second parameter represents the total length after alignment, and the third parameter represents the padded string (default is space).

For example:

<?php
$items = array(
    array(&#39;name&#39; => 'apple', 'price' => 10, 'count' => 3),
    array('name' => 'banana', 'price' => 15, 'count' => 2),
    array('name' => 'orange', 'price' => 8, 'count' => 4)
);

foreach($items as $item){
    echo str_pad($item['name'], 10, ' ', STR_PAD_RIGHT) 
        . str_pad($item['price'], 5, ' ', STR_PAD_LEFT)
        . str_pad($item['count'], 5, ' ', STR_PAD_LEFT) . "\n";
}
?>
Copy after login

The above code outputs the same result as the previous example.

  1. Use the table class library

For large amounts of data that need to be typed, using the table class library may be a better choice. The more commonly used table libraries include PHPExcel and phpMyAdmin. These table class libraries can generate tables in HTML or Excel format, and also support style and alignment operations.

For example, use the setAlignment function in PHPExcel to set the horizontal alignment:

$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
Copy after login

The above code means to align the content of cell A1 horizontally and centrally.

In short, for the alignment operation of the array, you can use PHP's printf/sprintf function, str_pad function, or use the table class library to achieve it. Which method to choose depends on the specific application scenario and personal preference.

The above is the detailed content of How to align php arrays. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template