在PHP

时间:2016-12-10 23:10:01

标签: php arrays

我试图在php和html中使用foreach循环生成动态数组。 到目前为止,我这样做:

<table class="tableau">
<thead><tr>
   <?php foreach ($lesFraisForfait as $unFrais){
            echo $libelle = $unFrais['libelle']; 
            echo " ";
         }
   ?>
 </tr></thead>
</table>

这是我设法得到的:

---------------------
name 1|name 2|name 3|
---------------------

$lesFraisForfait是一个包含3行nameidquantity的数组。

目标是生成一个数组,该数组接受每个名称并将其作为列名称,idname作为数组的左列。

--------------------------------
xxxxxxxxxx|name 1|name 2|name 3|
--------------------------------
id        | id 1 |id 2  | id 3 |
--------------------------------
quantity  | Q 1  | Q 2  | Q 3  |
--------------------------------

我该怎么做?

编辑:

var_dump($lesFraisForfait);

array (size=4)
  0 => 
    array (size=6)
      'idfrais' => string 'ETP' (length=3)
      0 => string 'ETP' (length=3)
      'libelle' => string 'Forfait Etape' (length=13)
      1 => string 'Forfait Etape' (length=13)
      'quantite' => string '0' (length=1)
      2 => string '0' (length=1)
  1 => 
    array (size=6)
      'idfrais' => string 'KM' (length=2)
      0 => string 'KM' (length=2)
      'libelle' => string 'Frais Kilométrique' (length=19)
      1 => string 'Frais Kilométrique' (length=19)
      'quantite' => string '0' (length=1)
      2 => string '0' (length=1)
  2 => 
    array (size=6)
      'idfrais' => string 'NUI' (length=3)
      0 => string 'NUI' (length=3)
      'libelle' => string 'Nuitée Hôtel' (length=14)
      1 => string 'Nuitée Hôtel' (length=14)
      'quantite' => string '0' (length=1)
      2 => string '0' (length=1)
  3 => 
    array (size=6)
      'idfrais' => string 'REP' (length=3)
      0 => string 'REP' (length=3)
      'libelle' => string 'Repas Restaurant' (length=16)
      1 => string 'Repas Restaurant' (length=16)
      'quantite' => string '0' (length=1)
      2 => string '0' (length=1)

3 个答案:

答案 0 :(得分:0)

这真的很容易。看看这个

<?php

    // helper for generating array
    // key should match key in $lesFraisForfait
    // value is a text displayed in table
    $order = [
        'idfrais' => 'id',
        'libelle' => 'name',
        'quantite' => 'quantity',
    ];

    /***
     * Foreach construct
     * - Added HTML character escaping to text
     * - Added unset to clear up variables after the foreach loops
     * - Added \n newlines at end of HTML rows for source formatting.
     ***/
    $output = "";
    foreach($order as $key => $text){
        $output .= "<td>".htmlspecialchars($text,ENT_QUOTES,false)."</td>\n";
        foreach($lesFraisForfait as $row){ 
             $output .="<td>".htmlspecialchars($row[$key],ENT_QUOTES,false)."</td>\n";
        }
        unset($row);
    }
    unset($key,$text);
?>

<table>
    <tr>
        <?php print $output;?>
    </tr>
</table>

答案 1 :(得分:0)

使用array_column

<?php 
$yourArray = array(
    array("name"=> "Name1", "id" => 1, "quantity" => 1),
    array("name"=> "Name2", "id" => 2, "quantity" => 2),
    array("name"=> "Name3", "id" => 3, "quantity" => 3),
    array("name"=> "Name4", "id" => 4, "quantity" => 4),
    array("name"=> "Name5", "id" => 5, "quantity" => 5),
);
?>
<div style="margin: 30px 10%;">
<table border="1" cellpadding="1" cellspacing="1" width="60%">
    <caption align="top">My Table</caption>
    <tr align="center">
        <?php foreach(array_column($yourArray,"name") as $name): ?>
            <th><?php echo $name; ?> </
        <?php endforeach; ?>
    </tr>
    <tr align="center">
        <?php foreach(array_column($yourArray,"id") as $id): ?>
            <td><?php echo $id; ?> </
        <?php endforeach; ?>
    </tr>
        <tr align="center">
                <?php foreach(array_column($yourArray,"quantity") as $quantity): ?>
            <td><?php echo $quantity; ?> </
        <?php endforeach; ?>
    </tr>
</table>

结果是:

My Table
Name1   Name2   Name3   Name4   Name5
 1        2      3       4        5
 1        2      3       4        5

答案 2 :(得分:0)

您可以像这样使用 input#field3 { width: auto; } implode

array_column