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

时间:2014-01-09 22:42:08

标签: php mysql

我正在尝试组合两个从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

试用&amp; (大部分)失败

如果我将公共功能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中它们根本没有被合并。

1 个答案:

答案 0 :(得分:2)

这里发生的是

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
        )

)
*/
相关问题