我有一個使用 phpspreadsheet 從 excel 檔案中獲取資料的回圈。資料獲取沒有問題,但我想做的是將資料組織成一個多維陣列,然后將其轉換為 JSON,以便我可以通過 javascript 訪問它。
這是代碼:
<?php require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Variables
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("test.xlsx"); // read existing spreadsheet
$worksheet = $spreadsheet->getSheetByName("table1"); // get specific sheet
$lastRow = $spreadsheet->getActiveSheet()->getHighestRow(); // get highest loaded row
$startRow = 3; // start cell to loop
$typeStart = 2; // start of type column
$column = [
"B",
"C",
"D",
"E",
"F"
];
$data = array();
for ($row = $startRow; $row <= $lastRow; $row ) {
$phoneType = $worksheet->getCell("A" . $typeStart)->getValue(); // get phone type
echo $phoneType . "<br>"; // display current phone type
$typeStart = $typeStart 6; // iterate to next phone type
$currentRow = $row; // add 2 to start value of issues
foreach ($column as $col) { // loop through the 5 columns
$compName = $worksheet->getCell($col . 1)->getValue();
echo "Company Name: " . $compName . "<br>";
for ($i = $currentRow; $i < $typeStart; $i ) {
$issueName = $worksheet->getCell("A" . $i)->getValue();
$issueCost = $worksheet->getCell($col . $i)->getValue();
$cost = array($issueName => $issueCost);
var_dump($cost);
echo "<br>";
}
}
$row = $row 5;
}
當我運行此代碼時,它會顯示以下文本:
IphoneX
Company Name: company1
array(1) { ["cracked"]=> int(7300) }
array(1) { ["LCD"]=> int(7400) }
array(1) { ["Water"]=> int(7500) }
array(1) { ["Data"]=> int(7600) }
array(1) { ["Network"]=> int(7700) }
Company Name: company2
array(1) { ["cracked"]=> int(8300) }
array(1) { ["LCD"]=> int(8400) }
array(1) { ["Water"]=> int(8500) }
array(1) { ["Data"]=> int(8600) }
array(1) { ["Network"]=> int(8700) }
Company Name: company3
array(1) { ["cracked"]=> int(7300) }
array(1) { ["LCD"]=> int(7400) }
array(1) { ["Water"]=> int(7500) }
array(1) { ["Data"]=> int(7600) }
array(1) { ["Network"]=> int(7700) }
Company Name: company4
array(1) { ["cracked"]=> int(8300) }
array(1) { ["LCD"]=> int(8400) }
array(1) { ["Water"]=> int(8500) }
array(1) { ["Data"]=> int(8600) }
array(1) { ["Network"]=> int(8700) }
Company Name: company5
array(1) { ["cracked"]=> int(7300) }
array(1) { ["LCD"]=> int(7400) }
array(1) { ["Water"]=> int(7500) }
array(1) { ["Data"]=> int(7600) }
array(1) { ["Network"]=> int(7700) }
IphoneXR
Company Name: company1
array(1) { ["cracked"]=> int(7300) }
array(1) { ["LCD"]=> int(7400) }
array(1) { ["Water"]=> int(7500) }
array(1) { ["Data"]=> int(7600) }
array(1) { ["Network"]=> int(7700) }
Company Name: company2
array(1) { ["cracked"]=> int(8300) }
array(1) { ["LCD"]=> int(8400) }
array(1) { ["Water"]=> int(8500) }
array(1) { ["Data"]=> int(8600) }
array(1) { ["Network"]=> int(8700) }
Company Name: company3
array(1) { ["cracked"]=> int(7300) }
array(1) { ["LCD"]=> int(7400) }
array(1) { ["Water"]=> int(7500) }
array(1) { ["Data"]=> int(7600) }
array(1) { ["Network"]=> int(7700) }
Company Name: company4
array(1) { ["cracked"]=> int(8300) }
array(1) { ["LCD"]=> int(8400) }
array(1) { ["Water"]=> int(8500) }
array(1) { ["Data"]=> int(8600) }
array(1) { ["Network"]=> int(8700) }
Company Name: company5
array(1) { ["cracked"]=> int(7300) }
array(1) { ["LCD"]=> int(7400) }
array(1) { ["Water"]=> int(7500) }
array(1) { ["Data"]=> int(7600) }
array(1) { ["Network"]=> int(7700) }
現在我想將它組織成一個如下所示的陣列:
{
"phoneType" : "IphoneX"
{
{
"companyName": "Company1",
"cracked" : 7300,
"LCD" : 7400,
"Water" : 7500,
"Data" : 7600,
"Network" : 7700
}
{
"companyName": "Company2",
"cracked" : 8300,
"LCD" : 8400,
"Water" : 8500,
"Data" : 8600,
"Network" : 8700
}
}
"phoneType" : "IphoneXR"
{
{
"companyName": "Company1",
"cracked" : 7300,
"LCD" : 7400,
"Water" : 7500,
"Data" : 7600,
"Network" : 7700
}
{
"companyName": "Company2",
"cracked" : 8300,
"LCD" : 8400,
"Water" : 8500,
"Data" : 8600,
"Network" : 8700
}
}
}
它可以實作嗎?提前致謝。
uj5u.com熱心網友回復:
foreach()您需要在回圈中使用指定的第一級鍵推送資料。不要費心為分組值(電話型別)提供鍵,只需將該值用作分組鍵。
foreach ($column as $col) {
$compName = $worksheet->getCell($col . 1)->getValue();
$set = ['companyName' => $compName]; // reset this subarray and populate with company name
for ($i = $currentRow; $i < $typeStart; $i ) {
$issueName = $worksheet->getCell("A" . $i)->getValue();
$issueCost = $worksheet->getCell($col . $i)->getValue();
$set[$issueName] = $issueCost;
}
$result[$phoneType][] = $set;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/513168.html
標籤:php数组循环
