简体   繁体   English

从 2 Arrays PHP 创建表

[英]Create table from 2 Arrays PHP

I Try to get a Table with 3 Rows in HTML.我尝试在 HTML 中获得一个包含 3 行的表。 The Data comes from a XML File and i put it in an array and parse that with foreach in the table.数据来自 XML 文件,我把它放在一个数组中,并用表中的 foreach 解析它。 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.问题是 Output 仅正确显示一个数组,而第二个数组仅显示 1 个 PRICE of 5。5 个不同的价格在数组“$recordpElements”中,我已经检查过了。 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.我不明白你的第 5 行代码。

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如果您运行此代码,那么您将在$recordpElements中获得array_slice($valuesp,0,5)的最后一条记录

So.. you will get the 5th record of $valuesp array in your $recordpElements .所以..您将在$recordpElements recordpElements 中获得$valuesp数组的第 5 条记录。

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:我不明白为什么价格数组( $valuesp )和产品数组( $valuesvb )应该分开存在,但假设这是正确的,如果产品序列和价格序列匹配,你应该尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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