【问题标题】:unable to merge variables with array_merge: undefined index无法将变量与 array_merge 合并:未定义的索引
【发布时间】:2014-01-09 22:42:08
【问题描述】:

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

有人建议我使用array_merge(),它似乎在大多数情况下都有效。从数据库返回所有结果后,我不断收到Warning: Illegal string offset。有趣的是,前 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 篇文章会按原样打印在页面上,但我得到了:

注意:未定义索引:第 74 行 /var/www/new-design/index.php 中的 tag_name

试验和(大部分)失败

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

警告:array_merge():参数 #2 不是第 67 行 /var/www/new-design/index.php 中的数组

警告:在第 67 行的 /var/www/new-design/index.php 中为 foreach() 提供的参数无效

我无法弄清楚如何在这方面取得成功,任何线索都会很棒。我从早上就开始了!

更新

article_tags 表

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

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

【问题讨论】:

  • 其中一个原因可能是未引用的WHERE article_uid = article_uid --- 你能试试WHERE article_uid = 'article_uid'WHERE article_uid = '$article_uid' 看看这是否与引号和/或$ 符号不同,因为您使用的是$first_uid = $data['article_uid'];
  • 我不认为array_merge 在这里做你想要的:假设$sth-&gt;db-&gt;fetchAll() 返回一个带有数字键的数组,你的结果数组只是一个附加到另一个数组。您可能想发布print_r($latestArticles)print_r($articleTags) 的结果(如果需要,请缩短它,以便您的帖子保持可读性),以便我们可以向您展示实际结果是什么(您可以告诉我们它应该是什么)。
  • @Fred-ii- 感谢您的建议,但只要正确分配引号,引号就不会影响结果。
  • 好的,谢谢。很高兴知道它得到了解决,干杯。 @AttisBarros

标签: php mysql


【解决方案1】:

这里发生的是

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
        )

)
*/

【讨论】:

  • 感谢您的插图,我正在更新问题以响应您的回答。正如我开始的那样,我现在将更新它。因此,正如我在更新中提到的那样,即使有标签的文章也没有 tag_name 因为它们没有合并,虽然我之前能够做到这一点,但我不记得它是如何实现的,但它是通过 array_merge 实现的. |编辑:我明白你在说什么,所以 array_merge 不会在这里为我服务,但我还没有找到另一种方法来完成这个..
  • 如果你只是想让你的标签信息和你的文章信息一起你可能想要改变你的查询latestArticles()以包含相应的标签信息(类似于你在@987654326中的做法@)。
  • 遗憾的是我试过了,即使使用 INNER JOIN 它似乎也不起作用,因为它给出了多个相同的结果。
  • article 表的article_uid 是否包含标签的id? (您的第二个函数表明,尽管从标准命名方案来看,我猜这是发布文章的人的用户 ID)。编辑:您可能只是在标签表下发布(一些,标题就足够了)您的文章表,以更好地显示这两者之间的联系。
  • 是的,您可以在这里看到。 article_tags 表i.imgur.com/L3waAJO.png 和文章表i.imgur.com/SVDHFQX.png
猜你喜欢
  • 2015-01-20
  • 2016-09-14
  • 2015-08-26
  • 2018-03-13
  • 1970-01-01
  • 2011-06-11
  • 2015-01-14
  • 1970-01-01
相关资源
最近更新 更多