0

A 有一个array这样的

Array
(
    [0] => stdClass Object
        (
            [user_id] => 3035
            [category] => Preliminary
            [value] => 0
            [fullname] => 
            [studid] => 1202924
            [remarks] => 
        )

    [1] => stdClass Object
        (
            [user_id] => 3035
            [category] => Midterm
            [value] => 0
            [fullname] => 
            [studid] => 1202924
            [remarks] => 
        )

    [2] => stdClass Object
        (
            [user_id] => 3035
            [category] => Semi-Finals
            [value] => 0
            [fullname] => 
            [studid] => 1202924
            [remarks] => 
        )

    [3] => stdClass Object
        (
            [user_id] => 3035
            [category] => Finals
            [value] => 0
            [fullname] => 
            [studid] => 1202924
            [remarks] => 
        )

    [4] => stdClass Object
        (
            [user_id] => 3035
            [category] => Remedial
            [value] => 0
            [fullname] => 
            [studid] => 1202924
            [remarks] => 
        )

    [5] => stdClass Object
        (
            [user_id] => 3036
            [category] => Preliminary
            [value] => 0
            [fullname] => 
            [studid] => 1202925
            [remarks] => 
        )

    [6] => stdClass Object
        (
            [user_id] => 3036
            [category] => Midterm
            [value] => 0
            [fullname] => 
            [studid] => 1202925
            [remarks] => 
        )

    [7] => stdClass Object
        (
            [user_id] => 3036
            [category] => Semi-Finals
            [value] => 0
            [fullname] => 
            [studid] => 1202925
            [remarks] => 
        )

    [8] => stdClass Object
        (
            [user_id] => 3036
            [category] => Finals
            [value] => 0
            [fullname] => 
            [studid] => 1202925
            [remarks] => 
        )

    [9] => stdClass Object
        (
            [user_id] => 3036
            [category] => Remedial
            [value] => 0
            [fullname] => 
            [studid] => 1202925
            [remarks] => 
        )

我想这样格式化

Array
(
    [0] => Array
        (
            [fullname] => 
            [studid] => 1202924
            [user_id] => 3035
            [Preliminary] => 0
            [Midterm] => 0
            [Semi-Finals] => 0
            [Finals] => 0
            [Remedial] => 0
        ),
    [1] => array
         (
            [fullname] =>
            [studid] => 1202925
            [user_id] => 3036
            [preliminary] => 0
            [midterm] => 0
            [semi-finals] => 0
            [finals] => 0
            [remedial] => 0
         )
)

基本上按他们studiduser_id

现在我尝试了很多尝试,比如

$count = 0;
$uid = $subject_student_grades[$count]->user_id;

for($x=0;$x < count($subject_student_grades);$x++)
{
    if($uid == $subject_student_grades[$x]->user_id)
    {
        $test[$count]['fullname'] = $subject_student_grades[$x]->fullname;
        $test[$count]['studid'] = $subject_student_grades[$x]->studid; 
        $test[$count]['user_id'] = $subject_student_grades[$x]->user_id; 
        $test[$count][$subject_student_grades[$x]->category] =      $subject_student_grades[$x]->value;
    }else{
        $count++;
    }
 }

$计数 = 0; foreach($subject_student_grades 作为 $value) {

    $uid = $subject_student_grades[$count]->user_id;

    if($uid == $value->user_id)
    {
        $test[$count]['fullname'] = $value->fullname;
        $test[$count]['studid'] = $value->studid; 
        $test[$count]['user_id'] = $value->user_id; 
        $test[$count][$value->category] = $value->value;

    }else{
        $count++;
    }
}

但问题是它只循环带有0索引的第一部分,并且在条件停止并且不处理或循环其余部分之后,注意指出我做错了什么?

编辑 所以现在我编辑它并将计数放在foreach loop仍然相同的唯一迭代之外,array我认为在它不恢复的条件之后?如果我删除它,else{}它输出的数组结构比我的目标差得多。

我试图echo在 else 上输出一些东西,echo's我认为这里的问题是,在循环进入条件后它返回 true,然后继续到 else 并且不会返回到if clause.

4

3 回答 3

1

$count关于第二个循环,肯定有一个错误:

foreach($subject_student_grades as $value)
{
    $count = 0; //Why is that here?

这样,每次迭代都会覆盖$countto 0,所以$test数组总是用 来寻址0,所以 element0将是那里唯一的一个。

这应该是这样的:

$count = 0;

foreach($subject_student_grades as $value)
{
    $uid = $subject_student_grades[$count]->user_id;
于 2013-02-21T10:23:00.370 回答
0

foreach 的重点是,除了可能构建一个新数组之外,您实际上不需要维护计数(但是您可以使用速记)。

我会这样做

$test = array();
foreach($subject_student_grades as $value) {
    $temp = array();
    $temp['fullname'] = $value->fullname;
    $temp['studid'] = $value->studid; 
    $temp['user_id'] = $value->user_id; 
    $temp[$value->category] = $value->value;

    $test[] = $temp; # This is shorthand to assign the next numerical index

}

if 语句似乎是多余的,因为它阻止了 count++ 当为 true 时,你的循环将完全不同步,因为 foreach 会自动向前移动,而你的 count 不会是第一次,所以总是会出现一次

于 2013-05-08T09:08:17.317 回答
0

计数必须在循环之外:

$count = 0;
foreach($subject_student_grades as $value)
{
    $uid = $subject_student_grades[$count]->user_id;

    if($uid == $value->user_id)
    {
        $test[$count]['fullname'] = $value->fullname;
        $test[$count]['studid'] = $value->studid; 
        $test[$count]['user_id'] = $value->user_id; 
        $test[$count][$value->category] = $value->value;

    }

    $count++;
}
于 2013-02-21T10:23:23.333 回答