如何将两个数组中的特定值组合到一个数组中,但有异常?

时间:2015-01-06 13:24:13

标签: php arrays

我有两个不同的数组

第一个阵列:

Array
(
    [0] => stdClass Object
        (
            [course_id] => 21
            [session_id] => 17
            [course_name] => Session 1 Course 1
            [course_description] => Session 1 Course 1
            [course_days] => 2
            [current_rate] => 1000
            [old_rate] => 
            [primary_tutor_id] => 18
            [secondary_tutor_id] => 25
            [additional_time] => 
            [location] => 
            [course_active] => 1

        )

    [1] => stdClass Object
        (
            [course_id] => 22
            [session_id] => 17
            [course_name] => Session 1 Course 2
            [course_description] => Session 1 Course 2
            [course_days] => 3
            [current_rate] => 1000
            [old_rate] => 
            [primary_tutor_id] => 24
            [secondary_tutor_id] => 25
            [additional_time] => 
            [location] => 
            [course_active] => 1

        )

    [2] => stdClass Object
        (
            [course_id] => 23
            [session_id] => 17
            [course_name] => Session 1 Course 3
            [course_description] => Session 1 Course 3
            [course_days] => 5
            [current_rate] => 2000
            [old_rate] => 
            [primary_tutor_id] => 26
            [secondary_tutor_id] => 27
            [additional_time] => 
            [location] => 
            [course_active] => 1

        )

    [3] => stdClass Object
        (
            [course_id] => 26
            [session_id] => 19
            [course_name] => Session 3 Course 2
            [course_description] => Session 3 Course 2
            [course_days] => 2
            [current_rate] => 400
            [old_rate] => 
            [primary_tutor_id] => 29
            [secondary_tutor_id] => 29
            [additional_time] => 
            [location] => 
            [course_active] => 1

        )

    [4] => stdClass Object
        (
            [course_id] => 27
            [session_id] => 19
            [course_name] => Session 3 Course 3
            [course_description] => Session 3 Course 3
            [course_days] => 1
            [current_rate] => 200
            [old_rate] => 
            [primary_tutor_id] => 26
            [secondary_tutor_id] => 
            [additional_time] => 
            [location] => 
            [course_active] => 1                
        )

)

第二阵列:

Array
(
    [0] => stdClass Object
        (
            [discount_id] => 104
            [session_id] => 17
            [no_course] => 3
            [discount] => 20                
        )

    [1] => stdClass Object
        (
            [discount_id] => 106
            [session_id] => 19
            [no_course] => 2
            [discount] => 20            

        )

)

我正在尝试将第二个数组的所有键和值组合到第一个数组中,如下面的示例

Array
(
    [0] => stdClass Object
        (
            [course_id] => 21
            [session_id] => 17
            [course_name] => Session 1 Course 1
            [course_description] => Session 1 Course 1
            [course_days] => 2
            [current_rate] => 1000
            [old_rate] => 
            [primary_tutor_id] => 18
            [secondary_tutor_id] => 25
            [additional_time] => 
            [location] => 
            [course_active] => 1
            [discount_id] => 104
            [session_id] => 17
            [no_course] => 3
            [discount] => 20

        )
)

我已经尝试过以下代码和许多其他代码,但我无法获得完美的解决方案。在这两个数组中,如果两个[session_id]与需要数组合并相同,则会有一个例外,因为它是没有合并的数组:

$main=array();
foreach ($courses as $key => $val) {    
            foreach ($session_discount as $se) {
                if ($se->session_id == $val->session_id)
                    $main[$key] = $val;
                array_push($main[$key], $session_discount[0]);
            }
        }

需要帮助..!在此先感谢。

0 个答案:

没有答案