Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php explode()存储从测试过滤器到索引[0]的所有值_Php_Arrays_Include_File Get Contents_Explode - Fatal编程技术网

php explode()存储从测试过滤器到索引[0]的所有值

php explode()存储从测试过滤器到索引[0]的所有值,php,arrays,include,file-get-contents,explode,Php,Arrays,Include,File Get Contents,Explode,我是一名学生,目前正在学习PHP,很难让explode()函数按我想要的方式工作 以下文本文件“vacations.txt”包含: "54321", "Big Island Hawaii", "2999.00", "Best beaches big volcano" "87654", "Cruise Caribbean", "3500.00", "Ocean view with balcony Cancun Jamaica etc." "09876", "Rome and Madrid", "3

我是一名学生,目前正在学习PHP,很难让explode()函数按我想要的方式工作

以下文本文件“vacations.txt”包含:

"54321", "Big Island Hawaii", "2999.00", "Best beaches big volcano"
"87654", "Cruise Caribbean", "3500.00", "Ocean view with balcony Cancun Jamaica etc."
"09876", "Rome and Madrid", "3999.00", "I see Italy I see Spain"
"32198", "Ski Tahoe", "1200.00", "Ski all day Gamble all night"
我尝试将其放入一个由逗号分隔的数组中。使用print_r(),我可以看到它全部进入数组的第一个索引。 这是我的密码:

function displaySpecials() {
    $prodString = include("vacation.txt");
    $vacArray = explode(',', $prodString ); 
        print_r($vacArray);
}
以下是输出:

“54321”、“夏威夷大岛”、“2999.00”、“最佳海滩大火山” “87654”、“加勒比邮轮”、“3500.00”、“带阳台的海景” 坎昆牙买加等“,”09876“,”罗马和马德里“,”3999.00“,”我明白了 意大利我看到西班牙“32198”,“滑雪塔霍”,“1200.00”,“滑雪一整天” “整夜赌博”数组([0]=>1)

我已经搜索并阅读了关于explode()的所有信息,但我不知道为什么会发生这种情况。我的最终目标是在一个有4行4列的表中输出一个多维数组,以显示文本文件中的16个值


非常感谢您的帮助。提前谢谢你

尝试用
文件获取内容(“vacation.txt”)
替换
包含(“vacation.txt”)

include语句包含并计算指定的文件

将整个文件读入字符串

您的代码应该如下所示:

function displaySpecials() {
    $prodString = file_get_contents("vacation.txt");
    $vacArray = explode(',', $prodString ); 
        print_r($vacArray);
}
if (($h = fopen('vacation.txt', 'r')) !== false) {
    while (($data = fgetcsv($h, 1000, ',')) !== false) {
        // Fetch the data here
    }
}

由于这基本上是一个
csv
文件,因此我建议使用如下内容:

function displaySpecials() {
    $prodString = file_get_contents("vacation.txt");
    $vacArray = explode(',', $prodString ); 
        print_r($vacArray);
}
if (($h = fopen('vacation.txt', 'r')) !== false) {
    while (($data = fgetcsv($h, 1000, ',')) !== false) {
        // Fetch the data here
    }
}
您还可以选择使用:

include语句包含并计算指定的文件

将评估其中的任何内容,基本上包括其他php脚本

您需要获取其内容

下面是我用来逐行获取文件内容的例子

function displaySpecials() {
    $lines = file("vacation.txt"); // will return lines in array
    foreach($lines as $line){ // loop for each line
        $vacArray[] = explode(",", $line); // insert resulting array to last index of $varArray
    } 
    print_r($vacArray);
    return $vacArray;
}

此外,该文件看起来是常规的
.csv
,您可以使用
fgetcsv
。检查官方文件中的示例

不太确定您的预期输出是什么,但不妨看看这里: