简体   繁体   中英

Create table from 2 Arrays PHP

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. 在此处输入图像描述

 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; }

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++;
        }

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