php - 数组函数产生错误
问题描述
下面的函数被设计为获取一个 .csv 文件,读取一列并将其值转换为数组。然后如果$category
- 在这种情况下是“打印机” - 出现在数组中,return "shipping-for-medium-sized-goods";
.
function get_ship_class($category){
$csv = array_map("str_getcsv", file("https://siliconharvest.com.au/cassius-files/Shipping%20Classes.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("medium_shipping_class", $header);
// Pass the extracted column back to calling method
return array_column($csv,$col);
if ( in_array( $category, get_ship_class() )) {
return "shipping-for-medium-sized-goods";
}
}
然后我运行该函数:
get_ship_class("printers");
// Note you can access the CSV file at that URL in the function above;
// See that"printers" is a value that occurs in the "medium_shipping_class" column
// Therefore we have a match for if ( in_array( $category = "printers", get_ship_class() ))
然而,我得到了错误:
警告:file() 期望参数 2 为整数,字符串在第 147 行的 /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php 中给出
警告:array_map():参数 #2 应该是第 147 行 /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php 中的一个数组
警告:array_shift() 期望参数 1 是数组,在第 148 行的 /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php 中给出了 null
警告:array_search() 期望参数 2 为数组,在第 151 行的 /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php 中给出了 null
警告:array_column() 期望参数 1 是数组,在第 154 行的 /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php 中给出了 null
解决方案
正如@Maio290 所说,错误非常明显:
您使用的是字符串"r"
而不是整数作为第二个参数:
http://php.net/manual/en/function.file.php
标志
您需要一个整数,或以下常量之一:
FILE_USE_INCLUDE_PATH
在 include_path 中搜索文件。我认为这是1
。
FILE_IGNORE_NEW_LINES
在每个数组元素的末尾省略换行符我认为这是2
.
FILE_SKIP_EMPTY_LINES
跳过空行。我认为这是3
。
解决这个问题可能会很好地解决其他错误,但如果没有,或者有任何问题,请告诉我,我会尽我所能扩展我的答案来帮助你。
推荐阅读
- javascript - 打字稿:遍历复选框(由ngfor制作)并打印每个复选框的值
- java - PostMapping 逻辑错误
- swift - SwiftUI 隐藏 UIKit UINavigationController(rootViewController: _) 的导航栏
- flutter - 制作元素之间没有间隙的网格视图
- java - 通过 Wi-Fi Direct 发送的图像已损坏
- xml - 通过 XML 解析器推送数据是否会根据本规范执行验证?
- sql-server - 为什么删除行在 SQL Server 中似乎很慢?
- go - 去安装复制权限被拒绝
- java - MediaRecorder 崩溃
- javascript - 试图优化早期的“Konami代码”功能