Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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关联数组的奇怪行为_Php_Arrays_Associative Array_Array Key - Fatal编程技术网

PHP关联数组的奇怪行为

PHP关联数组的奇怪行为,php,arrays,associative-array,array-key,Php,Arrays,Associative Array,Array Key,我使用的是一个关联数组,初始化如下: $img_captions = array(); f1.jpg|This is a caption for this specific file f2.jpg|Yea, also this one f3.jpg|And this too for sure ... if (file_exists($currentdir ."/captions.txt")) { $file_handle = fopen($currentdir ."/captions

我使用的是一个关联数组,初始化如下:

$img_captions = array();
f1.jpg|This is a caption for this specific file
f2.jpg|Yea, also this one
f3.jpg|And this too for sure
...
if (file_exists($currentdir ."/captions.txt"))
{
    $file_handle = fopen($currentdir ."/captions.txt", "rb");

    while (!feof($file_handle) )
    {
        $line_of_text = fgets($file_handle);
        $parts = explode('/n', $line_of_text);

        foreach($parts as $img_capts)
        {
            list($img_filename, $img_caption) = explode('|', $img_capts);
            $img_captions[$img_filename] = $img_caption;

        }
    }

    fclose($file_handle);
}
然后,在后面的代码中,我将使用.txt文件中的键和值(该.txt文件中的每一行都包含一对字符串,以“|”分隔),将其填充到while循环中,如下所示:

$img_captions = array();
f1.jpg|This is a caption for this specific file
f2.jpg|Yea, also this one
f3.jpg|And this too for sure
...
if (file_exists($currentdir ."/captions.txt"))
{
    $file_handle = fopen($currentdir ."/captions.txt", "rb");

    while (!feof($file_handle) )
    {
        $line_of_text = fgets($file_handle);
        $parts = explode('/n', $line_of_text);

        foreach($parts as $img_capts)
        {
            list($img_filename, $img_caption) = explode('|', $img_capts);
            $img_captions[$img_filename] = $img_caption;

        }
    }

    fclose($file_handle);
}
我用以下数据填充关联数组:

$img_captions = array();
f1.jpg|This is a caption for this specific file
f2.jpg|Yea, also this one
f3.jpg|And this too for sure
...
if (file_exists($currentdir ."/captions.txt"))
{
    $file_handle = fopen($currentdir ."/captions.txt", "rb");

    while (!feof($file_handle) )
    {
        $line_of_text = fgets($file_handle);
        $parts = explode('/n', $line_of_text);

        foreach($parts as $img_capts)
        {
            list($img_filename, $img_caption) = explode('|', $img_capts);
            $img_captions[$img_filename] = $img_caption;

        }
    }

    fclose($file_handle);
}
当我测试关联数组时,如果它实际上包含键和值,如:

print_r(array_keys($img_captions));
print_r(array_values($img_captions));
…我看到它像预期的那样包含它们,但是当我尝试将它们用于直接调用时,比如说:

echo $img_captions['f1.jpg'];
我发现PHP错误是这样说的:

注意:未定义索引:f1.jpg在…

我不知道这里发生了什么事,有人能告诉我吗

顺便说一句,我正在使用USBWebserver和PHP5.3

更新1:因此,通过更好地探索“打印(数组)键($img_字幕))的输出;”在Chrome(F12键)内部,我注意到一些奇怪的东西-第一行“[0]=>f1.jpg”在视觉上看起来非常奇怪虽然它在网站上显示为print_r()输出时看起来很正常,但我注意到它实际上在网页源代码(F12)中是这样编码的:

所以当我测试1以外的任何东西时。行,它工作正常。我试图完全删除该文件并再次重新写入,但仍然发生相同的情况

免责声明伙计们,为了更准确地澄清一些事情:这不是我的原始代码(完全由我完成),它是 事实上,我刚刚制作了一个MiniGal NanoPHP照片集,以适合我的需要 需要,但我们正在讨论的那些特定部分是 原作者


我会推荐你和水一起使用

您的代码变得简短、可读且易于理解

$parts= file('your text file url', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$img_captions = [];
foreach($parts as $img_capts){
    list($img_filename, $img_caption) = explode('|', $img_capts);
    $img_captions[trim(preg_replace("/&#?[a-z0-9]+;/i","",$img_filename))] = trim(preg_replace("/&#?[a-z0-9]+;/i","",$img_caption));
}
print_r($img_captions);

因此,过了一会儿,我意识到我的
.txt
文件本身有问题,因为它:-

无论我做什么,总是在第一行前面放一些奇怪的标记,即使是从头开始创建的新文件,它也总是在那里(尽管这些标记是不可见的,除非在网页上作为源代码出现!!!)

所以我决定用另一种格式测试它,这次是.log文件,突然间一切都正常了

我不知道这是否只是我当地的某种问题(很可能是),还是我不知道的其他问题

但我的解决方案是更改保存字符串对的文件类型(.txt=>.log),这为我解决了这个“问题”

一些
其他可能的解决方案如@Abracadver所说:


(那些奇怪的符号:[0]=>&65279;f1.jpg)这是字节顺序标记或BOM的HTML实体,请保存您的文件 在您使用的任何编辑器中都没有BOM表


print_r(数组键($img_字幕))的输出是什么?您的代码有几个问题:
'/n'
应该是
“\n”
(错误的引号,错误的斜杠)。首先不需要在换行符上分解,因为
fgets()
只读取一行。我敢打赌,实际的问题是文件中的文件名字段前后都有空格。这是字节顺序标记或BOM的HTML实体,在您使用的任何编辑器中保存您的文件,但不包含BOM表。或者它以其他方式到达那里。
trim()
可能是他缺少的解决方案。@Barmar我的想法完全相同,但我有一个比OP的代码更好的主意。所以我把这个想法和
trim()