gpt4 book ai didi

php - 无法使用 array_merge 合并变量 : undefined index

转载 作者:行者123 更新时间:2023-11-29 08:13:47 24 4
gpt4 key购买 nike

我正在尝试组合两个从 MySQL 检索信息的变量。

有人建议我使用array_merge(),它似乎在大多数情况下都有效。从数据库返回所有结果后,我不断收到警告:非法字符串偏移。有趣的是,前 8 个(查询的 LIMIT 为 8)是错误清除的,在打印 8 个结果后,会出现一个包含该错误的巨大列表。

查询

articleClass.php

public function latestArticles()
{
$sth = $this->db->prepare("SELECT * FROM articles
WHERE article_uid = article_uid
ORDER BY article_uid DESC LIMIT 8");
$sth->execute();
$row = $sth->fetchAll();
return $row;
}

public function articleTags()
{
$sth = $this->db->prepare("SELECT a.*, b.*
FROM articles a, article_tags b
WHERE b.tag_id = a.article_uid
");
$sth->execute();
$row = $sth->fetchAll();
return $row;
}

打印代码

index.php

include 'libraries/articleClass/articleClass.php';

$articleClass = new articleClass();
$latestArticles = $articleClass->latestArticles();
$articleTags = $articleClass->articleTags();

foreach(array_merge($latestArticles, $articleTags) as $data)
{
$first_uid = $data['article_uid'];
$first_image = $data['article_image'];
$first_title = $data['article_title'];
$first_content = $data['article_content'];
$first_created = gmdate("d M Y", $data['article_created']);
$first_tags = $data['tag_name'];

echo '
<article>
<img src="path-to-image/'.$first_image.'"/>
<h1>'.$first_title.'</h1>
<p>'.$first_content.'</p>
<ul>
<li>'.$first_tags.'</li>
</ul>

</article>
';
}

加载 index.php 后,页面上会按预期打印 8 篇文章,但我得到:

Notice: Undefined index: tag_name in /var/www/new-design/index.php on line 74

试验和(大部分)失败

如果我将公共(public)函数article_tags更改为Fetch而不是FetchAll,我会收到以下错误:

Warning: array_merge(): Argument #2 is not an array in /var/www/new-design/index.php on line 67

Warning: Invalid argument supplied for foreach() in /var/www/new-design/index.php on line 67

我不知道如何成功,任何线索都会很棒。我从早上就开始了!

更新

article_tags表

+--------------------------------+
| tag_id | article_id | tag_name |
+--------------------------------+
| 1 | 8 | awesome |
| 2 | 8 | sweet |
| 3 | 8 | gross |
+--------------------------------+

被调用的文章只有一个article_id对应,但这篇文章仍然收到未定义的index: tag_name,当然是因为在array_merge中它们根本没有被合并。

最佳答案

这里发生的事情是这样的

array_merge($latestArticles, $articleTags)

简单地将一个数组附加到另​​一个数组,意思是:结果数组中的前 8 个条目是您的文章(这些条目设置了“tag_name”字段)。

其余部分填充了 $articleTags 的内容(那些没有“tag_name”字段 - 导致您的错误)。

下面是一些代码来说明这一点(索引 0 到 2 是您的文章,而 3 到 5 是您的标签,请注意结果数组的索引 3-5 没有 tag_name 字段):

$latestArticles = array(
array("article_title" => "Some Title 1", "tag_name" => "SomeTag"),
array("article_title" => "Some Title 2", "tag_name" => "SomeOtherTag"),
array("article_title" => "Some Title 3", "tag_name" => "SomeTag"),
);

$articleTags = array(
array("name" => "SomeTag", "somefield" => "foo", "otherfield" => "bar"),
array("name" => "SomeTagOtherTag", "somefield" => "baz", "otherfield" => "test"),
array("name" => "YetAnotherTag", "somefield" => "test2", "otherfield" => "test3")
);


$result = array_merge($latestArticles, $articleTags);
print_r($result);

/** Resulting Array:
Array
(
[0] => Array
(
[article_title] => Some Title 1
[tag_name] => SomeTag
)

[1] => Array
(
[article_title] => Some Title 2
[tag_name] => SomeOtherTag
)

[2] => Array
(
[article_title] => Some Title 3
[tag_name] => SomeTag
)

[3] => Array
(
[name] => SomeTag
[somefield] => foo
[otherfield] => bar
)

[4] => Array
(
[name] => SomeTagOtherTag
[somefield] => baz
[otherfield] => test
)

[5] => Array
(
[name] => YetAnotherTag
[somefield] => test2
[otherfield] => test3
)

)
*/

关于php - 无法使用 array_merge 合并变量 : undefined index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com