简体   繁体   中英

Generate dynamic array in PHP

I'm trying to generate a dynamic array with a foreach loop in php and html. so far I'm doing this:

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

here is what i managed to get:

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

$lesFraisForfait is an array that contains 3 rows name , id ,and quantity .

The goal is to generate an array that takes each name and puts it as the column name and id , name as the left column of the array.

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

How can I do this ?

EDIT:

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)

Thats really easy. Just look at this

<?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>

Using 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>

Result is:

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

You could use implode and array_column like this:

<table class="tableau">
<thead>
    <tr><th>xxx</th><th><?=implode("</th><th>",array_column($lesFraisForfait, "libelle"))?></th></tr>
</thead>
<tbody>
    <tr><td>id frais</td><td><?=implode("</td><td>",array_column($lesFraisForfait, "idfrais"))?></td></tr>
    <tr><td>quantité</td><td><?=implode("</td><td>",array_column($lesFraisForfait, "quantite"))?></td></tr>
</tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM