gpt4 book ai didi

php - 在 PHP 中的函数之间传递数组

转载 作者:行者123 更新时间:2023-12-04 05:46:53 26 4
gpt4 key购买 nike

我是 PHP 新手,我正在从 Excel 电子表格中读取数据,我想将每一行保存为一个数组。在我拥有该数组后,我想将该数组传递给另一个函数,以使用特定标题“设置”每个数组值。我无法弄清楚这一点,希望有人能提供帮助。

这是第一个脚本:

    <?php 
include './Classes/PHPExcel.php';
include 'testCase.php';

class ExcelReader{

function getExcelFile(){
/*Get file from form*/
echo 'Hello world' . "\n";
$FileName = $_FILES["fileName"]["name"];

/*Move file to server, if file already exists, don't move*/
if(file_exists("uploadedFiles/".$_FILES["fileName"]["name"])){
echo $_FILES["fileName"]["name"]." already exists";
}
else{
move_uploaded_file($_FILES["fileName"]["tmp_name"],"uploadedFiles/".$_FILES["fileName"]["name"]);
echo $_FILES["fileName"]["name"]." has been moved";



}
return $FileName;
}

function readExcelFile($FileName){


/*Create reader object for file and read object in*/
$PHPExcelReader= PHPExcel_IOFactory::createReaderForFile($FileName);
$PHPExcelReader->setReadDataOnly(true);
$PHPExcelObj=$PHPExcelReader->load($FileName);






$PHPobjWorksheet = $PHPExcelObj->getActiveSheet();

$topRow = $PHPobjWorksheet->getHighestRow();
$topCol = $PHPobjWorksheet->getHighestColumn();

$highestColIndex= PHPExcel_Cell::columnIndexFromString($topCol);
for($row=1;$row<=$topRow; $row++){

$newTestCase = new testCase();
$testIndex = $newTestCase->getTestCase($row, $highestColIndex, $PHPobjWorksheet);
echo $testIndex."<- Test Index <br/>";
$newTestCase->setTestCase($testIndex);
}
}
}

$newExcelReader = new ExcelReader;
$newFileName = $newExcelReader->getExcelFile();
$newExcelReader->readExcelFile($newFileName);

?>

这是我用来获取数据的第二个类,我正在尝试设置:
    <?php

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Class for creating test cases to be sent
* into TestRail
* @author Banderson
*/
class testCase {

/*Function to get the data for each individual test case*/
function getTestCase($row, $highestColIndex, $PHPobjWorksheet){

echo "<br />";
for($col=0;$col<=$highestColIndex;++$col){
$ii=0;
$cellValue=array($PHPobjWorksheet->getCellByColumnAndRow($col,$row)->getValue());
echo $cellValue[$ii]. " | ";
$ii++;


}

return $cellValue;

}


/*Function to set the data for each individual test case*/
function setTestCase($cellValue){
$title = $cellValue[0];
echo $title. "<- Title"."<br/>";
$type = $cellValue[1];
echo $type. "<- Type"."<br/>";
$priority = $cellValue[2];
echo $priority. "<- Priority"."<br/>";
$estimate = $cellValue[3];
echo $estimate. "<- Estimate"."<br/>";
$milestone = $cellValue[4];
echo $milestone. "<- MileStone"."<br/>";
$references = $cellValue[5];
echo $references. "<- 5"."<br/>";
$preconditions = $cellValue[6];
echo $preconditions. "<- 6"."<br/>";
$steps = $cellValue[7];
echo $steps. "<- 7"."<br/>";
$expectedResults = $cellValue[8];
echo $expectedResults. "<- 8"."<br/>";
$testSuit = $cellValue[9];
echo $testSuit. "<- 9"."<br/>";


}



}

?>

最后这是我收到的错误消息:
    Hello world testrailtestinputV2.xlsx already exists
Title | Type | Priority | Estimate | Milestone | Reference | Preconditions | Steps | Expected Result | Section | Test Suite | | Array<- Test Index<- Title

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 37
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- Type

( ! ) Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 39
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- Priority

( ! ) Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 41
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- Estimate

( ! ) Notice: Undefined offset: 4 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 43
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- MileStone

( ! ) Notice: Undefined offset: 5 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 45
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- 5

( ! ) Notice: Undefined offset: 6 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 47
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- 6

( ! ) Notice: Undefined offset: 7 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 49
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- 7

( ! ) Notice: Undefined offset: 8 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 51
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- 8

( ! ) Notice: Undefined offset: 9 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 53
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.3133 5664536 testCase->setTestCase( ) ../testRailScripting.php:50
<- 9

Title 1 | Functionalit | 1 | | | | | 1. Turn phone on 2. Go to main menu 3. Push button | The screen should look like this | | | | Array<- Test Index<- Title

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 37
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- Type

( ! ) Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 39
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- Priority

( ! ) Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 41
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- Estimate

( ! ) Notice: Undefined offset: 4 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 43
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- MileStone

( ! ) Notice: Undefined offset: 5 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 45
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- 5

( ! ) Notice: Undefined offset: 6 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 47
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- 6

( ! ) Notice: Undefined offset: 7 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 49
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- 7

( ! ) Notice: Undefined offset: 8 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 51
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- 8

( ! ) Notice: Undefined offset: 9 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 53
Call Stack
# Time Memory Function Location
1 0.0000 635280 {main}( ) ../testRailScripting.php:0
2 0.0034 749112 ExcelReader->readExcelFile( ) ../testRailScripting.php:57
3 0.5097 5675280 testCase->setTestCase( ) ../testRailScripting.php:50
<- 9

Title 2 | Functionalit | 1 | | | | | 1. Turn phone on 2. Go to main menu 3. Push button | The screen should look like this | | | | Array<- Test Index<- Title

任何帮助都会很棒!先感谢您!

最佳答案

您的职能 getTestCase创建一个包含一个元素的新数组并将其放入变量 $cellValue在每次迭代中。所以,这个函数返回的数组只包含一个元素。您应该以这种方式修复它:

function getTestCase($row, $highestColIndex, $PHPobjWorksheet) {
$cellValue = array();
for($col = 0; $col <= $highestColIndex; ++$col) {
$v = $PHPobjWorksheet->getCellByColumnAndRow($col,$row)->getValue();
$cellValue[] = $v; //append element to the array
}
return $cellValue;
}

关于php - 在 PHP 中的函数之间传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com