Home > Software engineering >  Create table from 2 Arrays PHP
Create table from 2 Arrays PHP

Time:12-05

I Try to get a Table with 3 Rows in HTML. The Data comes from a XML File and i put it in an array and parse that with foreach in the table. The problem is the Output show only one array correctly and the second array show only 1 PRICE of 5. The 5 different Prices are in the array "$recordpElements", i have check it. What im doing wrong? See the example in the Picture below.enter image description here

public function xmlParserVB():string
  { 
        global $obj;
 
        $valuesvb = $this->xml->xpath("OBJEKT[@ID='$obj']//SAISON");
        $valuesp = $this->xml->xpath("//OBJEKT[@ID='$obj']//SAISON//PRICE");
        
        foreach (array_slice($valuesp,0,5) as $recordpElements);
        foreach (array_slice($valuesvb,0,5) as $recordvbElements)
        
         {
            $display .= '<tr>';
            $display .= '<td>'.$recordvbElements->DESCRIPTION.'</td>';
            $display .= '<td>'.$recordvbElements->FROM.' - '.$recordvbElements->UNTIL.'</td>';
            $display .= '<td>'.$recordpElements->PRICE.' &euro;</td>';
            $display .= '</tr>';
        
    
        }
        
     $display .= '';

      return $display;
        
  }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

[![enter image description here][2]][2]

CodePudding user response:

I don't understand your 5th line of code.

foreach (array_slice($valuesp,0,5) as $recordpElements);

if you run this code then you will get last record of array_slice($valuesp,0,5) in $recordpElements

So.. you will get the 5th record of $valuesp array in your $recordpElements.

I don't understand why the price array($valuesp) and product array($valuesvb) should exist separately, but assuming that's correct, if the product sequence and price sequence match, you should try something like this:

        $price = array_slice($valuesp,0,5);
        $index = 0;
        foreach (array_slice($valuesvb,0,5) as $recordvbElements)
        {

            $display .= '<tr>';
            $display .= '<td>'.$recordvbElements->DESCRIPTION.'</td>';
            $display .= '<td>'.$recordvbElements->FROM.' - '.$recordvbElements->UNTIL.'</td>';
            $display .= '<td>'.$price[$index]->PRICE.' &euro;</td>';
            $display .= '</tr>';
        
           $index  ;
        }
  • Related