将SQL附加到现有关联数组

时间:2016-11-13 05:35:42

标签: php associative-array

学习PHP并创建关联数组。

尝试创建描述活动的关联数组。此示例的活动为354和355.

这是我目前的代码:

$query = $db->prepare('SELECT item_id FROM kh_program_item_info WHERE '. $where);
    $query->execute();
    if($query->rowCount() > 0){
        while($john = $query->fetch(PDO::FETCH_OBJ)){
            $key1 = $john->item_id;
            $report-> $key1= array();
        }


        foreach ($report as $key=>$johnval) {

            $where = 'item_id = ' . $key;       
            $query = $db->prepare('SELECT activity_desc, DATE_FORMAT(activity_date,"%d/%m/%Y") AS activityDate FROM kh_program_items WHERE '. $where);
            $query->execute();
            $results = $query->fetch(PDO::FETCH_OBJ);
            $report->$key = $results;


            echo '<pre>';
            echo print_r($report);
            echo '</pre>';          


        }

    } else {
        echo '<p>there are no activities</p>';
    }

这将产生结果。

stdClass Object
(
    [354] => stdClass Object
        (
            [activity_desc] => <p>Send Activity</p>
            [activityDate] => 08/11/2016
        )

    [355] => stdClass Object
        (
            [activity_desc] => <p>Send Activity 2</p>
            [activityDate] => 11/11/2016
        )

)

我现在正在尝试为354和355添加更多信息 我重新访问了代码,这里是新代码:

$query = $db->prepare('SELECT item_id FROM kh_program_item_info WHERE '. $where);
    $query->execute();
    if($query->rowCount() > 0){
        while($john = $query->fetch(PDO::FETCH_OBJ)){
            $key1 = $john->item_id;
            $report-> $key1= array();
        }


        foreach ($report as $key=>$johnval) {

            $where = 'item_id = ' . $key;       
            $query = $db->prepare('SELECT activity_desc, DATE_FORMAT(activity_date,"%d/%m/%Y") AS activityDate FROM kh_program_items WHERE '. $where);
            $query->execute();
            $results = $query->fetch(PDO::FETCH_OBJ);
            $report->$key = $results;

            $query = $db->prepare('SELECT info_value FROM kh_program_item_info WHERE '. $where .' AND info_type="program_category"');
            $query->execute();
            $info_value = $query->fetch(PDO::FETCH_OBJ);

            $where = 'category_id = ' . $info_value->info_value;
            $query = $db->prepare('SELECT category_name FROM kh_program_categories WHERE '. $where);
            $query->execute();
            $actcategories = $query->fetch(PDO::FETCH_OBJ); 

            $report->$key = $actcategories;         

            echo '<pre>';
            echo print_r($report);
            echo '</pre>';          
        }

    } else {
        echo '<p>there are no activities</p>';
    }

这显然会覆盖我创建的内容并产生以下结果:

stdClass Object
(
    [354] => stdClass Object
        (
            [category_name] => Parent Input
        )

    [355] => stdClass Object
        (
            [category_name] => Children's Menu
        )

)

问题是$myactivity->$key = $actcategories;

如何在不覆盖信息的情况下追加到最后?

我需要得出这个结果

stdClass Object
    (
        [354] => stdClass Object
            (
                [activity_desc] => <p>Send Activity</p>
                [activityDate] => 08/11/2016
                [category_name] => Parent Input
            )

        [355] => stdClass Object
            (
                [activity_desc] => <p>Send Activity 2</p>
                [activityDate] => 11/11/2016
                [category_name] => Children's Menu
            )

    )

提前致谢。

2 个答案:

答案 0 :(得分:0)

您需要先构建数组,然后将其发送到对象。

即。 :

    foreach ($report as $key=>$johnval) {

        $where = 'item_id = ' . $key;       
        $query = $db->prepare('SELECT activity_desc, DATE_FORMAT(activity_date,"%d/%m/%Y") AS activityDate FROM kh_program_items WHERE '. $where);
        $query->execute();
        // build first array
        $result_first_query = $query->fetch(PDO::FETCH_OBJ);

        $query = $db->prepare('SELECT info_value FROM kh_program_item_info WHERE '. $where .' AND info_type="program_category"');
        $query->execute();
        $info_value = $query->fetch(PDO::FETCH_OBJ);

        $where = 'category_id = ' . $info_value->info_value;
        $query = $db->prepare('SELECT category_name FROM kh_program_categories WHERE '. $where);
        $query->execute();
        // build second array
        $result_second_query = $query->fetch(PDO::FETCH_OBJ); 

        // put them together in $report->$key
        $report->$key = $result_first_query + $result_second_query;         

        echo '<pre>';
        print_r($report);
        echo '</pre>';          
    }

希望它有所帮助。

答案 1 :(得分:0)

我在使用fetch_assoc后确实设法推送。

我想这只是它不喜欢的格式,但是谢谢你。