0

参照を使用して配列を変更しています:

foreach($uNewAppointments as &$newAppointment)
{
    foreach($appointments as &$appointment)
    {
        if($appointment == $newAppointment){
            $appointment['index'] = $counter;
        }
    }
    $newAppointment['index'] = $counter;
    $newAppointments[$counter] = $newAppointment;

    $counter++;
}

配列の内容を出力すると、期待どおりの結果が得られます。それを繰り返すと、すべての要素が同じように見えます (最初の要素)。

内部配列の参照演算子&を削除すると、インデックスが設定されていないことを除いて、すべて正常になります。

4

2 に答える 2

5

foreach ループで参照を使用すると問題が発生します :) 私はそれを数回行い、常にそのコードを書き直しました。

あなたもそれにすべきです。このような:

foreach($uNewAppointments as $newAppointmentKey => $newAppointment)
{
        foreach($appointments as $appointmentKey => $appointment)
        {
                if($appointment == $newAppointment){
                        appointments[$appointmentKey]['index'] = $counter;
                }
        }
        $uNewAppointments[$newAppointmentKey]['index'] = $counter;
        $$uNewAppointments[$newAppointmentKey][$counter] = $newAppointment;

        $counter++;
}

「機械的に」書き直したばかりですが、おそらく動作しないでしょう。しかし、それは副作用なしで同じ効果を達成する方法のアイデアを得ることです. このループでは、まだ元の配列を変更しています。

于 2009-10-02T14:59:20.293 に答える
4

これを行う場合、ループを終了するときに $newAppointment の設定を解除する必要があります。関連するエントリは次のとおりです。

于 2009-10-02T14:56:11.173 に答える