php - php页面中的非法字符串偏移警告
问题描述
我目前正在编辑一个名为“test.php”的文档,想上传一个csv文件,读取其中的数据,并在这个页面上输出一些信息。
我有这个 html 代码:
<html>
<head></head>
<body>
<table width="600">
<form action="" method="post" enctype="multipart/form-data">
<tr>
<td width="20%">Select CSV file</td>
<td width="80%"><input type="file" name="filename" id="file" /></td>
</tr>
<tr>
<td width="20%">Select Model file</td>
<td width="80%"><input type="file" name="dontworry" id="file" /></td>
</tr>
<tr>
<td>Submit</td>
<td><input type="submit" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
我也有这个 php 命令:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// get value from csv file
$csv = array();
$file = fopen($_FILES['filename']['tmp_name'], "r");
while (($result = fgetcsv($file)) !== false)
{
$csv[] = $result;
}
fclose($file);
// set non repeat value by a list
$onedarray = array_2d_to_1d($csv);
$onedarraynoneempty = array_merge(array_unique(array_filter($onedarray, "myFilter")));
$arraynum = array_merge(removeNum($onedarraynoneempty));
$counts = array_count_values($onedarray);
// get assiciation and print out the associate result
for ($i = 0; $i < count($arraynum)-1; $i++){
/*
echo '<pre>';
print_r($arraynum[$i]);
echo '</pre>';
*/
$newlist = array_merge(removeActual($arraynum, $i));
/*
echo '<pre>';
print_r($newlist);
echo '</pre>';
*/
count_support($csv, $arraynum, $arraynum[$i], $counts[$arraynum[$i]], $newlist);
}
/*
echo '<pre>';
print_r($arraynum);
print_r($arraynum[2]);
echo '</pre>';
*/
}
// finding connection between the other product and $item in one line, if it has 3 line connection, output the message
function count_support($arr, $count, $item, $totalcount, $newlist){
for ($b = 0; $b < count($newlist); $b++){
$finalcount = 0;
for ($i = 0; $i < count($arr); $i++) {
for ($j = 0; $j < count($arr[$i]); $j++) {
if ($newlist[$b] != null){
$compare = $newlist[$b];
if($arr[$i][$j][$item] != null && $arr[$i][$j][$compare] != null){
$finalcount ++;
}
}
}
}
//output each result in the support and confidence
$confidence = 3 / $totalcount;
if($finalcount = 3)
{
$part = $newlist[$b];
echo $item . " -> " . $part . "Support: 3, Confidence: " . $confidence . "<br/ >";
}
}
}
// change 2d array into 1d array
function array_2d_to_1d($input_array) {
$output_array = array();
for ($i = 0; $i < count($input_array); $i++) {
for ($j = 0; $j < count($input_array[$i]); $j++) {
$output_array[] = $input_array[$i][$j];
}
}
return $output_array;
}
// ignore null element in the array
function myFilter($var){
return ($var !== NULL && $var !== FALSE && $var !== "");
}
// remove number index in 1d array
function removeNum($var){
for ($i = 0; $i < count($var); $i++) {
if (is_numeric($var[$i])) {
unset($var[$i]);
}
}
return $var;
}
// remove on element in an array
function removeActual($var, $listnum){
unset($var[$listnum]);
return $var;
}
?>
这是我的 csv 文件,我想将整个 csv 文件元素保存到一个二维数组中,并遍历整个数组,在页面上输出等于 3 的两个产品之间的支持连接。大致效果如下:
blueberry -> pear, Support: 3, Confidence: 0.75
pear -> blueberry, Support: 3, Confidence: 1
blueberry -> tang celery, Support: 3, Confidence: 0.75
tang celery -> blueberry, Support: 3, Confidence: 0.75
lotus root -> tomato red, Support: 3, Confidence: 0.6
tomato red -> lotus root, Support: 3, Confidence: 0.75
pear -> tang celery, Support: 3, Confidence: 1
tang celery -> pear, Support: 3, Confidence: 0.75
coriander -> spinach, Support: 3, Confidence: 0.6
orange -> spinach, Support: 3, Confidence: 0.6
persimmon -> spinach, Support: 3, Confidence: 0.6
luzon mango -> persimmon, Support: 3, Confidence: 0.75
persimmon -> luzon mango, Support: 3, Confidence: 0.6
但是当我最终按下提交时,跳出许多行:
( ! ) Warning: Illegal string offset 'Tomato Red' in C:\wamp64\www\test.php on line 84
Call Stack
# Time Memory Function Location
1 0.0037 411368 {main}( ) ...\test.php:0
2 0.0059 470080 count_support( ) ...\test.php:60
( ! ) Warning: Illegal string offset 'Lemon Yellow' in C:\wamp64\www\test.php on line 84
Call Stack
# Time Memory Function Location
1 0.0037 411368 {main}( ) ...\test.php:0
2 0.0059 470080 count_support( ) ...\test.php:60
这个错误无限增长并最终导致网站崩溃。这是我在函数count_support中的第 84 行:
if($arr[$i][$j][$item] != null && $arr[$i][$j][$compare] != null){
我想知道如何改进我的 php 代码以使这个错误消失。
解决方案
推荐阅读
- angular - Observable 源对象被破坏时的内存泄漏
- android - Firebase 不存储数据。项目构建成功,在 Logcat 中没有错误
- javascript - 米利斯秒JS二分查找
- python - 将 CVXPY 向量与内部具有参数的数组进行比较的问题
- jquery - jquery在插入文本后立即设置插入焦点
- html - 在类上使用媒体查询
- r - 使用 tidyverse 对 R 中的多列进行加权和
- ruby-on-rails - Rspec/FactoryBot 读取我之前的 schema.db 文件
- python - 如何根据数据框中的值删除数据框中的某些条目
- excel - 从第一个到最后一个单元格的动态