数组格式化打印表

时间:2016-06-10 18:50:05

标签: php arrays printing

我有循环,例如:

foreach($cars as $car){
    $html .= $car[name];
    $html .= $car[color];
    $html .= $car[doors];
}

它像桌子一样打印:

Mercedes | red | 3
Mercedes | blue | 3
Mercedes | red | 5
Ford | green | 4
Ford | green | 5

如何打印:

Mercedes 
red | 3
blue | 3
red | 5

Ford
green | 4
green | 5

请给我一些提示。

4 个答案:

答案 0 :(得分:1)

您需要做的就是记住最后一个车名以及何时更改输出车名。

$last_car = NULL;
foreach($cars as $car){
    if ( $last_car != $car['name'] ) {
        $html .= $car[name] . '<br>';
        $last_car = $car['name'];
    }
    $html .= $car[color] . '|';
    $html .= $car[doors] . '<br>';
}

答案 1 :(得分:1)

你可以试试这个:

$temps = array();
$html = '';
$i = 0;
foreach($cars as $car){
   $temps[$car['name']][$i]['color'] = $car['color']; 
   $temps[$car['name']][$i]['doors'] = $car['doors']; 
   $i++;
}
foreach($temps as $tmp=>$val){
        $html .= $tmp."<br>";
    foreach($val as $v){
        $html .= $v['color']." | ";
        $html .= $v['doors'];
        $html .= '<br>';
    }
}
print $html;

答案 2 :(得分:1)

让我们看看我们最初的汽车数组

Array(
[0] => Array
    (
        [name] => Ford
        [color] => Red
        [door] => 3
    )

[1] => Array
    (
        [name] => Toyota
        [color] => Yellow
        [door] => 4
    )

[2] => Array
    (
        [name] => Toyota
        [color] => White
        [door] => 3
    )

[3] => Array
    (
        [name] => Toyota
        [color] => Yellow
        [door] => 3
    )

[4] => Array
    (
        [name] => Mercedes
        [color] => Black
        [door] => 4
    )

[5] => Array
    (
        [name] => Lexus
        [color] => Yellow
        [door] => 4
    )

[6] => Array
    (
        [name] => Mercedes
        [color] => White
        [door] => 2
    )

[7] => Array
    (
        [name] => Mercedes
        [color] => Black
        [door] => 4
    )

[8] => Array
    (
        [name] => Mercedes
        [color] => Black
        [door] => 4
    )

[9] => Array
    (
        [name] => Toyota
        [color] => Black
        [door] => 3
    )

[10] => Array
    (
        [name] => Toyota
        [color] => White
        [door] => 3
    )

)

预期输出

Ford
Red | 3

Toyota
Yellow | 4
White | 3
Yellow | 3
Black | 3
White | 3

Mercedes
Black | 4
White | 2
Black | 4
Black | 4

Lexus
Yellow | 4

实现此结果的代码

        <?php
    $carsArray = hydrateCarArray(); // You can ignore this as you already have carsArray this is just to hydrate inital array.
    $cleanCarsArray = filterCarsByName($carsArray); // This function sort cars by name
    $html = getRequiredFormat($cleanCarsArray); // this hydrate html var with required strcuture
    echo $html;

    /*
     * In your case this function is not required as you already have car array
     */
    function hydrateCarArray()
    {
        $carsName = [
            'Mercedes',
            'Toyota',
            'Lexus',
            'Ford',
            'Ferari'
        ];
        $carsColors = [
            'Red', 'White', 'Black', 'Yellow'
        ];
        for ($counter = 0; $counter <= 10; $counter++) {
            $carsArray[] = [
                'name'  => $carsName[ rand(0, count($carsName) - 1) ],
                'color' => $carsColors[ rand(0, count($carsColors) - 1) ],
                'door'  => rand(2, 4)
            ];
        }

        return $carsArray;
    }
    /*
     * Arrange cars array by cars name
     */
    function filterCarsByName($carsArray)
    {
        $cleanCarsArray = [];
        foreach ($carsArray as $car) {
            $cleanCarsArray[ $car['name'] ][] = $car;
        }

        return $cleanCarsArray;
    }
    /*
     * This function put the car details in required format.
     */
    function getRequiredFormat($cleanCarsArray)
    {
        $html = '';
        array_walk($cleanCarsArray, function ($item, $key) use (&$html) {
            $html .= "<br/>" . $key . "<br/>";
            array_walk($item, function ($item, $key) use (&$html) {
                $html .= $item['color'] . ' | ' . $item['door'] . "</br>";
            });
        });

        return $html;
    }
?>

答案 3 :(得分:0)

在网络上我发现了这个:

`foreach($features as $key => $feature){
{
$html .= '<table style=" margin:0 20px; border:0;">';
$_features = $feature['features'];
{
if(!isset($features[$key-1]) || $feature['groupname'] != $features[$key-1]     ['groupname'])
$html .= '<tr><td colspan="2">'.$feature['groupname'].'</td></tr>';
$html .= '<tr>';
$html .= '<td style="padding: 3px 5px 3px 0;width:230px;float:     left;background-color: #F4F4F6; font-family: Tahoma, Geneva, sans-serif; font-    size:16px; text-align: right;">'.$feature['name'].' </td>';
$html .= '<td style="padding: 3px 0px 3px 10px;width: 400px;float: left;background-color:#ECECEC; font-family: Tahoma, Geneva, sans-serif; font-size:16px; font-weight: bold;text-align: left;">'.$feature['value'].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
}`

它帮助我解决了我的问题。