donglijuan8227 2014-01-09 22:42
浏览 86
已采纳

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

I am trying to combine two variables that retrieves information from MySQL.

I have been suggested to use array_merge(), and it seems to work for the most part. I keep getting Warning: Illegal string offset after all the results has been returned from the database. Interesting is that the first 8 (the query has a LIMIT of 8) are error clean, after the 8 results has been printed, then a huge list appears with that error.

query

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;
}

printing code

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>
';
}

Once index.php is loaded, 8 articles are printed on the page as they should, but I get :

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

Trial & (mostly) Failures

If I change public function article_tags to Fetch instead of FetchAll I get these errors:

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

I am unable to figure out how to succeed with this, any leads would be great. I've been at it since morning!

UPDATE

article_tags table

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

there is only one article_id corresponding to the articles that are being called, yet this article still receives undefined index: tag_name of course because in the array_merge they haven't been merged at all.

  • 写回答

1条回答 默认 最新

  • dthyxna3894 2014-01-09 22:55
    关注

    What happens here is that

    array_merge($latestArticles, $articleTags)
    

    simply appends one array to the other, meaning: The first 8 entries in the resulting array are your articles (these have the "tag_name" field set).

    The rest is filled with the contents of $articleTags (those don't have "tag_name" field - causing your error).

    Here's some code to illustrate that (Indices 0 to 2 are your articles while 3 to 5 are your tags, notice how indices 3-5 of the resulting array don't have the tag_name field):

    $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
            )
    
    )
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办