我有以下代码:
$lines;
$invalidNumber = [];
$validNumber = [];
function readCSV($thefile) {
$i = 0;
$file = fopen($thefile, 'r');
// Put numbers from CSV into array
while (($line = fgetcsv($file)) !== FALSE) {
// Write lines to array
$lines[$i] = $line;
$i++;
}
fclose($file);
validateNumbers($lines);
}
// Go through array sorting numbers into invalidNumber and validnumbers
function validateNumbers($lines) {
// Valid numbers togo to reuqests and invalid to csvFile to be displayed seperately
$validNumber = [];
$invalidNumber = [];
for ($i = 0; $i < count($lines); ++$i) {
echo "</br>";
$number[$i] = $lines[$i][0];
echo $number[$i] . " length: " . strlen($number[$i]);
/*
switch on length of number: , ,
*/
switch (strlen($number[$i])) {
case 10:
if (substr($number[$i], 0, -9) != 7) {
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
} else {
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
}
break;
case 11:
switch (substr($number[$i], 0, -9)) {
case 07:
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
default:
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
break;
}
break;
case 12:
switch (substr($number[$i], 0, -9)) {
case 447:
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
case '077':
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
default:
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
break;
}
break;
case 13:
switch (substr($number[$i], 0, -9)) {
case '+447':
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
default:
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
break;
}
break;
default:
// if number is not 10, 11, 12 or 13 charaters long if < 10 (SKIP), if = 10 & charAt 1 != 7 (invalid)
echo "Invalid Number";
break;
}
}
}
我想将函数$ validNumber和$ invalidNumber传递给我在页面顶部声明的数组,无论我如何尝试,我都无法做到。
答案 0 :(得分:1)
首先将数组作为参数传递给函数,使它们成为IN-SCOPE
现在,如果您还使用&
引用运算符,那么您将传递对原始数组的引用,而不是将这个数组的副本传递给这两个函数。因此,无论你对它们做什么都将对数组本身进行,而不是数组的副本。
function readCSV($thefile, &$invalidNumber, &$validNumber, &$lines) {
$i = 0;
$file = fopen($thefile, 'r');
// Put numbers from CSV into array
while (($line = fgetcsv($file)) !== FALSE) {
// Write lines to array
$lines[$i] = $line;
$i++;
}
fclose($file);
validateNumbers(&$lines, $invalidNumber, $validNumber);
}
// Go through array sorting numbers into invalidNumber and validnumbers
function validateNumbers($lines, &$invalidNumber, &$validNumber, &$lines) {
// Valid numbers togo to reuqests and
// invalid to csvFile to be displayed seperately
for ($i = 0; $i < count($lines); ++$i) {
echo "</br>";
$number[$i] = $lines[$i][0];
echo $number[$i] . " length: " . strlen($number[$i]);
/*
switch on length of number: , ,
*/
switch (strlen($number[$i])) {
case 10:
if (substr($number[$i], 0, -9) != 7) {
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
} else {
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
}
break;
case 11:
switch (substr($number[$i], 0, -9)) {
case 07:
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
default:
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
break;
}
break;
case 12:
switch (substr($number[$i], 0, -9)) {
case 447:
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
case '077':
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
default:
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
break;
}
break;
case 13:
switch (substr($number[$i], 0, -9)) {
case '+447':
$vnc = count($validNumber);
$validNumber[$vnc++] = $number[$i];
break;
default:
$inc = count($invalidNumber);
$invalidNumber[$inc++] = $number[$i];
break;
}
break;
default:
// if number is not 10, 11, 12 or 13 charaters long if < 10 (SKIP), if = 10 & charAt 1 != 7 (invalid)
echo "Invalid Number";
break;
}
}
}
$lines = [];
$invalidNumber = [];
$validNumber = [];
readCSV('the_file_name', $invalidNumber, $validNumber, $lines);