Php 继续获取未定义的索引错误
我对这个问题简直快发疯了 我用PHP编写了以下代码:Php 继续获取未定义的索引错误,php,html,Php,Html,我对这个问题简直快发疯了 我用PHP编写了以下代码: private function searchpage($data) { $file = @file_get_contents("./assets/template/search.html"); $content = ""; $pages = ""; foreach ($data as $keys) { //var_dump($keys); This gives the correct information,I wil
private function searchpage($data) {
$file = @file_get_contents("./assets/template/search.html");
$content = "";
$pages = "";
foreach ($data as $keys) {
//var_dump($keys); This gives the correct information,I will show the
output later on.
$content = str_replace("[PHOTO]",$keys['photo'], $file);
$content = str_replace("[NAME]",$keys['name'], $content);
$pages .= $content;
}
return $pages;
}
$keys的var_转储如下所示
array(1) { [0]=> array(7) { ["_id"]=> string(1) "2" ["_user"]=>
string(1) "1" ["_short"]=> string(4) "test" ["_long"]=> string(9)
"test long" ["name"]=> string(4) "test" ["_dat"]=> string(10) "2017-
08-29" ["photo"]=> string(92) "https://s-media-cache-
"}
如您所见,我使用了正确的名称标记
但是stru替换不起作用。我不断地发现这个错误:
Undefined index: name in
/Applications/MAMP/htdocs/test/lib/_layout.php on line 144
Undefined index: photo in /Applications/MAMP/htdocs/test/lib/_layout.php on line 145
奇怪的是:我在不同的HTML页面上做了完全相同的事情,在那里它工作得非常完美
我不知道这个代码出了什么问题。
我确实要求这个功能。我提供给函数的数据是正确的。它确实打开了正确的HTML页面。唯一不正确的是str_replace。您的
var_dump
表示$keys
包含一个数组
。因此,在PHP中,如您所知,如果您试图访问数组变量,则还必须指定索引
因此,它应该是
$keys[0]['photo']
,而不是$keys['photo']
。类似地,它应该是$keys[0]['name']
,而不是$keys['name']
请更换
$content = str_replace("[PHOTO]",$keys['photo'], $file);
$content = str_replace("[NAME]",$keys['name'], $content);
与
可能是重复的我已经看到了,但还没有找到解决我问题的方法。尝试替换
$content=str_替换(“[PHOTO]”,$keys['PHOTO'],$file);$content=str_替换(“[NAME]”,$keys['NAME'],$content);
为$content=str_替换(“[PHOTO]”,$keys[0]['PHOTO'],$file);$content=stru替换(“[NAME]“,$keys[0]['name'],$content)代码>。var\u dump
显示,$keys
保存一个数据数组。$keys[0]['name']
尝试这种方法,但请在注释中回答。如果在这里回答这个问题足够好,那么就可以做出一个回答。
$content = str_replace("[PHOTO]",$keys[0]['photo'], $file);
$content = str_replace("[NAME]",$keys[0]['name'], $content);