COMBINE 3 ARRAYS IN PHP

0

Hi, I'm trying to combine 3 arrays into one, but it can only be done with two arrays.

Array ( [0] => ITEM1 [1] => ITEM2 [2] => )  
Array ( [0] => 2 [1] => 4 [2] => )  
Array ( [0] => 50 [1] => 400 [2] => )   

How could I combine them so that the first array was the key and be able to extract them in a table in the following way?

<table border='1'>
<tr>
<td>Concepto</td>
<td>Unidades</td>
<td>Precio</td>
</tr>
<tr>
<td>ITEM 1</td>
<td>2</td>
<td>50</td>
</tr>
<tr>
<td>ITEM 2</td>
<td>4</td>
<td>400</td>
</tr>
</table>
    
asked by Fran Rod 18.08.2018 в 12:48
source

4 answers

1

If it is a representation of the data (to show them in a table), I do not see how it makes sense to waste effort and resources in placing the name of every column in the element of the array. Such a practice could have performance consequences, even disastrous if the array is very large.

A better solution would be:

  • Keep an independent array with what would be the names of the columns in the table
  • Combine the arrays with data in a very simple way: creating a new array with all of them! and then do what is known as transposition , with the help of array_map . That is, we will make the different elements of each array are placed in their respective column.
  • We will use two loops foreach to build our final table.
  • The code would be this:

    <?php
        $arrItems=array("Item1","Item2","Item n...");
        $arrUnidades=array(2,4,999);
        $arrPrecios=array(50,400,999);
    
        $arrColumns=array("Concepto","Unidades","Precio");
    
        $arrCombinado = array($arrItems,$arrUnidades,$arrPrecios);
        $arrOrdenado = transposeData($arrCombinado);
        $strHTML="<table>";
        $strHTML.="<thead>";
        $strHTML.="<tr>";
        #Columnas
        foreach ($arrColumns as $column){
            $strHTML.="<th>$column</th>";
        }
        $strHTML.="</tr>";
    
        #Filas
        foreach ($arrOrdenado as $v){
            $strHTML.="<tr>";
            foreach($v as $row){
                #Celdas
                $strHTML.="<td>$row</td>";
            }
            $strHTML.="</tr>";
        }
        $strHTML.="</table>";
        echo $strHTML;
    
        #Función para hacer la transposición
        function transposeData($data){
            return array_map(function (...$line){
                return $line;
            }, ...$data);
        }
    
    ?>
    

    Exit:

    We're going to get a table like this:

    <link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css" rel="stylesheet"/>
        <table class="table is-striped">
            <thead>
                <tr>
                    <th>Concepto</th>
                    <th>Unidades</th>
                    <th>Precio</th>
                </tr>
                <tr>
                    <td>Item1</td>
                    <td>2</td>
                    <td>50</td>
                </tr>
                <tr>
                    <td>Item2</td>
                    <td>4</td>
                    <td>400</td>
                </tr>
                <tr>
                    <td>Item n...</td>
                    <td>999</td>
                    <td>999</td>
                </tr>
            </thead>
        </table>

    Demonstration

    Here you can see a DEMONSTRATION OF THE CODE and make tests. p>

    Reference

    You can see the article Transposition in PHP that has served as basis for this response.

        
    answered by 19.08.2018 в 09:32
    0

    Well I have already found the solution, it would be creating a single array with the three uncoordinated arrays:

    foreach($arrayConcepto as $key => $valores)
     {
    $files[$key]['concepto'] = $valores;
    $files[$key]['unidades'] = $arrayUnidades[$key];
    $files[$key]['precioGestion'] = $arrayPrecio[$key];
     }
    

    To get the values simply:

       echo "<TABLE width='auto' border='1'>";
    foreach ($files as $key2 => $value2) {
    
        echo "<tr>";
        echo "<td>";
        echo $value2['concepto'];
        echo "</td>";
        echo "<td>";
        echo $value2['unidades'];
        echo "</td>";
        echo "<td>";
        echo $value2['precioGestion'];
        echo "</td>";
        echo "</tr>";
       }
     echo "</table>";
    
        
    answered by 18.08.2018 в 13:21
    0

    Simple Array
    Simply structure them as if it were a database and add line by line:

    $datos[] =["Item1",2,50];
    $datos[] = ["Item2",4,400];
    

    Or both of them from the tiron:

     $datos = array(
          array("Item1",2,50),
          array("Item2",4,400) 
     );
    

    Then write your values:

    <tr>
      <th>Concepto</th>
      <th>Unidades</th>
      <th>Precio</th>
    <tr>
    <?php
      foreach($datos as $linea){
        echo "<tr>";
        foreach($linea as $dato){
          echo "<td>$dato</td>";
        }
        echo "</tr>";
      };
    
    ?>
    

    Associative Array
    If you need to be clear about what each item is in the array, you can name each index:

    $datos = array(
       array("Concepto"=> "Item1", "Unidades"=>2, "Precio"=>50),
       array("Concepto"=> "Item2", "Unidades"=>4, "Precio"=>400)
    )
    

    And to write:

    <tr>
      <th>Concepto</th>
      <th>Unidades</th>
      <th>Precio</th>
    <tr>
    <?php
      foreach($datos as $linea){
        echo "<tr>";
          echo "<td>".$linea["Concepto"]."</td>";
          echo "<td>".$linea["Unidades"]."</td>";
          echo "<td>".$linea["Precio"]."</td>";
        echo "</tr>";        
      };  
    ?>
    
        
    answered by 18.08.2018 в 13:40
    0

    The problem is with the structure of the arrangements. You should make an associative array that will be as follows:

    $items = array(
        0 => array(
                'concepto' => 'ITEM 1',
                'unidades' => '2',
                'precio' => 50
            ),
        1 => array(
                'concepto' => 'ITEM 2',
                'unidades' => '4',
                'precio' => 400
            ),
    );
    

    If you can not arm it with this new structure directly, the simplest way to do it is by foreach the three arrangements;

    foreach($miArray as $key => $value){
         $items[$key]['concepto'] = $value; //Para el primer Array
         $items[$key]['unidades'] = $value; //Para el segundo Array
         $items[$key]['precio'] = $value; //Para el tercer Array
    }
    

    and then simply make a table as follows:

    <table class="table table-striped">
        <thead>
            <tr>
                <th>Concepto</th>
                <th>Unidades</th>
                <th>Precio</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($items as $item){?>
            <tr>
                <td><?php echo $item['concepto'];?></td>
                <td><?php echo $item['unidades'];?></td>
                <td><?php echo $item['precio'];?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    
        
    answered by 19.08.2018 в 09:47