无法在多维数组中获取主题

时间:2014-05-06 12:43:56

标签: php

$student = array();
$subjects = array();
foreach($this->items as $std)
{
    foreach($this->items as $course)
    {
        $subjects[$course->course_title] = $course->obtain_marks;
    }
    $student[$std->std_id]  = array($std->std_id, $std->name,$subjects);
   }

输出:它显示我相同的主题结果而不是挑选第一个学生的结果请帮助...........输出如下............ 。

Array
(
    [1] => Array
        (
            [0] => 1
            [1] => ayan
            [2] => Array
                (
                    [english] => 43
                    [urdu] => 55
                    [IQ] => 25
                    [OP] => 15
                    [nazra] => 15
                    [G.K] => 15
                    [maths] => 70
                )

        )

    [2] => Array
        (
            [0] => 2
            [1] => bilal
            [2] => Array
                (
                    [english] => 43
                    [urdu] => 55
                    [IQ] => 25
                    [OP] => 15
                    [nazra] => 15
                    [G.K] => 15
                    [maths] => 70
                )

        )

)

我怎样才能得到两个结果................

3 个答案:

答案 0 :(得分:0)

你还没有发布很多上下文,因为你使用嵌套循环我假设你试图从子数组中获取值。基于此,您的foreach循环中存在问题。它应该是下面的东西。

foreach($this->items as $std)
{
    foreach($std as $course) //It should be $std here instead of $this->items.
    {

    }

}

答案 1 :(得分:0)

更改结果数组键值

foreach($this->items as $std)
{
    foreach($coursearray as $course)
    {
        $subjects[$course->course_title] = $course->obtain_marks;
    }
    $student[]  = array('standard_id'=>$std->std_id,'standard_name'=>$std->name,'subjects'=>$subjects);
   }

答案 2 :(得分:0)

倾向于我的评论以及Jay Bhatt的帖子......

在这里你必须看看你的对象是否包含数组

$student = array();
$subjects = array();

foreach($this->items as $std)
{
    foreach($std as $course) //It should be $std here instead of $this->items.
    {
        $subjects[$course['course_title'] = $course['obtain_marks'];
    }
    $student[$std->std_id]  = array($std->std_id, $std->name, $subjects);
}

或subObjects

$student = array();
$subjects = array();

foreach($this->items as $std)
{
    foreach($std as $course) //It should be $std here instead of $this->items.
    {
        $subjects[$course->course_title] = $course->obtain_marks;
    }
    $student[$std->std_id]  = array($std->std_id, $std->name, $subjects);
}