PHP将数组组织为产品详细信息

PHP将数组组织为产品详细信息,php,arrays,sorting,Php,Arrays,Sorting,大家好,我在阵列问题上需要一些帮助 我有一个从PDF文本创建的平面数组 该数组是与其他页面文本混合的产品数据 看来这种模式是正确的 描述深度 然后X个产品的数量 描述符 产品ID 价格 数据也不一致,有些产品超过3行 一个产品将是 说明:Hemsworth高/低液位盘400(高)x 375(宽)x 438(深)毫米 产品编号:INST02009 价格:176.00英镑 我在一个类似的项目中使用了它,但它不太管用 $transactions = array(); foreach ($arr

大家好,我在阵列问题上需要一些帮助

我有一个从PDF文本创建的平面数组 该数组是与其他页面文本混合的产品数据 看来这种模式是正确的

描述深度

然后X个产品的数量

  • 描述符
  • 产品ID
  • 价格
数据也不一致,有些产品超过3行

一个产品将是

  • 说明:Hemsworth高/低液位盘400(高)x 375(宽)x 438(深)毫米
  • 产品编号:INST02009
  • 价格:176.00英镑
我在一个类似的项目中使用了它,但它不太管用

$transactions = array();
foreach ($array as $row) {
    if ($row['0'] === "DescriptionCodePrice") {
        $transactions[] = array();
     }
     $transactions[count($transactions) - 1][0] = $row;
 }
我试图将所有产品数据提取到一个整洁的数组中,就像这样

  Array
(
    [products] => Array
        (
            [0] => Array
                (
                    [description] => 
                    [id] => 
                    [price] => 
                 )

            [1] => Array
                (
                    [description] => 
                    [id] => 
                    [price] => 
                )

            [2] => Array
                (
                    [description] => 
                    [id] => 
                    [price] => 
                )

         )

 )
这是我的数据

Array
(
    [0] => 8        SANITARYWARE  |  HEMSWORTH CLOSE COUPLEDPrices include VAT  
    [1] => DescriptionCodePrice
    [2] => Hemsworth Close Coupled Pan 
    [3] => 421(h) x 373(w) x 673(d) mm INST02007
    [4] => £206.00 
    [5] => Hemsworth Close Coupled 
    [6] => Cistern 481(w) mm INST02001
    [7] => £170.00 
    [8] => Hemsworth Basin 605mm  
    [9] => Two Taphole INST02003
    [10] => £172.00
    [11] => Hemsworth Pedestal INST02008£84.00
    [12] => Hemsworth Soft Close Bar 
    [13] => Hinge Seat - Solid Natural Oak INST02011
    [14] => £147.00
    [15] => Total £779.00   
    [16] => Hemsworth Close Coupled WC Suite
    [17] => Description CodePrice
    [18] => Hemsworth Close Coupled Pan 
    [19] => 421(h) x 373(w) x 673(d) mm INST02007
    [20] => £206.00
    [21] => Hemsworth Close Coupled 
    [22] => Cistern 481(w) mm INST02001
    [23] => £170.00
    [24] => Hemsworth Soft Close Bar 
    [25] => Hinge Seat - Solid Natural Oak INST02011
    [26] => £147.00
    [27] => Hemsworth Soft Close Bar 
    [28] => Hinge Seat - White INST02012
    [29] => £132.00 
    [30] => Hemsworth   
    [31] => Hemsworth Basin 605mm
    [32] => Description CodePrice
    [33] => Hemsworth Basin 605mm  
    [34] => Two Taphole INST02003
    [35] => £172.00
    [36] => Hemsworth Basin 605mm  
    [37] => One Taphole INST02010
    [38] => £172.00
    [39] => Hemsworth Cloakroom Basin 
    [40] => 500 x 305mm Two Taphole  INST02013
    [41] => £144.00
    [42] => Hemsworth Pedestal (Fits 
    [43] => 605mm and 500mm basin) INST02008
    [44] => £84.00  
    [45] => £256.   00  
    [46] => Hemsworth Basin 
    [47] => 605mm Two Taphole   
    [48] => & Pedestal   
    [49] => (Taps not included) 
    [50] => £523.   00  
    [51] => Hemsworth Close  Coupled WC with  Oak Seat  
    [52] => Hemsworth Suite with Close  
    [53] => Coupled Cistern WC & Basin  
  )
我当前的页面代码是

require_once("vendor/autoload.php"); 

// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf    = $parser->parseFile('Instinct-Autumn-Bathroom-Catalogue-2018- 
pages/page-9.pdf');

// Retrieve all pages from the pdf file.
$pages  = $pdf->getPages();

// Loop over each page to extract text.
foreach ($pages as $page) {
    $array = explode("\n", $page->getText());
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    echo '<br><br>';
}

// Split array into transactions
$transactions = array();
foreach ($array as $row) {
  if ($row['0'] === "DescriptionCodePrice") {
      $transactions[] = array();
  }
  $transactions[count($transactions) - 1][0] = $row;
}
require_once(“vendor/autoload.php”);
//解析pdf文件并构建必要的对象。
$parser=new\Smalot\PdfParser\parser();
$pdf=$parser->parseFile('Institute-秋-Bathy-Catalog-2018-
pages/page-9.pdf');
//从pdf文件中检索所有页面。
$pages=$pdf->getPages();
//在每页上循环以提取文本。
foreach($页为$页){
$array=explode(“\n”,$page->getText());
回声“;
打印(数组);
回声“;
回音“

”; } //将数组拆分为事务 $transactions=array(); foreach($array作为$row){ if($row['0']==“descriptionDeprice”){ $transactions[]=array(); } $transactions[计数($transactions)-1][0]=$row; }
这是一种方法:

首先删除标题。(看起来您实际上有两个不想要的标题,而不仅仅是“DescriptionDeprice”。)

将剩余的数据分成三个块,并将每个块与字符串键组合以生成结果


首先,你是如何提取数据的?我使用,它以文本形式给我PDF中的数据,然后将每一行添加到一个数组中。这几乎可以正常工作,它只是添加了额外的不需要的数据。一些产品超过3行,一些超过4行,因为这种不一致性,它不准确,因为数据是混合的,写这篇文章时,我没有注意到数组中额外的行。我正要去吃午饭,但过一会儿我再看一遍。:-)非常感谢。我已经看过其他我必须做的PDF页面,它们是一种类似的格式,只是每个产品行上的数量可以给你更多的想法这是每个PDF页面提取到数组中,你可以看到不一致性,我想我将不得不调整每页解析的PDF的代码
array_splice($data, 0, 2);
$keys = ['title', 'description', 'price'];

$result['products'] = array_map(function ($item) use ($keys) {
    return array_combine($keys, $item);
}, array_chunk($data, 3));