Ich habe Code als,

    $totals = [];

    foreach ($student_due_fee as $key => $fee) {
    foreach ($fee->fees as $fee_key => $fee_value) {

    $group_id = $fee_value->fee_groups_id;

    if(!isset($totals[$group_id])){

       $totals[$group_id] = $fee_value->amount;
       }else{

       $totals[$group_id] += $fee_value->amount;
       print_r($totals);
     }
   }
  }

Und die Ausgabe ist,

Array ( [69] => 3000 ) 
Array ( [69] => 6000 ) 
Array ( [69] => 6000 [70] => 3500 ) 
Array ( [69] => 6000 [70] => 4500 ) 

Hier muss ich den letzten Wert als Endergebnis erhalten, nämlich

Array ( [69] => 6000 [70] => 4500 ) 

Wie kann ich den letzten Array-Wert erhalten, ohne die foreach-Schleife zu verlassen?

Bild des Ergebnisses,

enter image description here

Hier mache ich die Summe des Betrags basierend auf der Gruppe und speichere sie in $ Summen in foreach und ich muss das Endergebnis anzeigen, das kommt, wenn ich print_r($totals) mache, das Array ( [69] => 6000 [70] => 4500 ) ist.

Ausgabe von $ fee-> Gebühren ist,

Array ( [0] => stdClass Object ( [id] => 19 [student_session_id] => 278 [fee_session_group_id] => 15 [is_active] => no [created_at] => 2018-04-17 05:18:41 [fee_groups_feetype_id] => 74 [amount] => 1000.00 [due_date] => 2018-04-24 [fee_groups_id] => 69 [name] => Ist Term - LKG [feetype_id] => 1 [code] => ISEMTUTION [type] => I SEM TUTION FEES [student_fees_deposite_id] => 0 [amount_detail] => 0 ) [1] => stdClass Object ( [id] => 19 [student_session_id] => 278 [fee_session_group_id] => 15 [is_active] => no [created_at] => 2018-04-17 05:18:41 [fee_groups_feetype_id] => 75 [amount] => 2000.00 [due_date] => 2018-04-24 [fee_groups_id] => 69 [name] => Ist Term - LKG [feetype_id] => 2 [code] => IITUTION [type] => II SEM TUTION FEES [student_fees_deposite_id] => 0 [amount_detail] => 0 ) [2] => stdClass Object ( [id] => 19 [student_session_id] => 278 [fee_session_group_id] => 15 [is_active] => no [created_at] => 2018-04-17 05:18:41 [fee_groups_feetype_id] => 76 [amount] => 3000.00 [due_date] => 2018-04-24 [fee_groups_id] => 69 [name] => Ist Term - LKG [feetype_id] => 3 [code] => SPORTS [type] => SPORTS FEES [student_fees_deposite_id] => 0 [amount_detail] => 0 ) ) 

Array ( [0] => stdClass Object ( [id] => 65 [student_session_id] => 278 [fee_session_group_id] => 16 [is_active] => no [created_at] => 2018-04-17 05:20:20 [fee_groups_feetype_id] => 77 [amount] => 2000.00 [due_date] => 2018-04-24 [fee_groups_id] => 70 [name] => IInd Term - LKG [feetype_id] => 7 [code] => MAGAZINE [type] => MAGAZINE, PHOTOS,BAG, ID etc [student_fees_deposite_id] => 0 [amount_detail] => 0 ) [1] => stdClass Object ( [id] => 65 [student_session_id] => 278 [fee_session_group_id] => 16 [is_active] => no [created_at] => 2018-04-17 05:20:20 [fee_groups_feetype_id] => 78 [amount] => 1500.00 [due_date] => 2018-04-24 [fee_groups_id] => 70 [name] => IInd Term - LKG [feetype_id] => 8 [code] => SCHOLASTICS [type] => CO - SCHOLASTICS [student_fees_deposite_id] => 0 [amount_detail] => 0 ) [2] => stdClass Object ( [id] => 65 [student_session_id] => 278 [fee_session_group_id] => 16 [is_active] => no [created_at] => 2018-04-17 05:20:20 [fee_groups_feetype_id] => 79 [amount] => 1000.00 [due_date] => 2018-04-24 [fee_groups_id] => 70 [name] => IInd Term - LKG [feetype_id] => 9 [code] => BOOKSUNIFORM [type] => BOOKS, NOTEBOOKS, UNIFORM [student_fees_deposite_id] => 0 [amount_detail] => 0 ) ) 

Und die Ausgabe von $ fee_value ist,

stdClass Object ( [id] => 19 [student_session_id] => 278 [fee_session_group_id] => 15 [is_active] => no [created_at] => 2018-04-17 05:18:41 [fee_groups_feetype_id] => 74 [amount] => 1000.00 [due_date] => 2018-04-24 [fee_groups_id] => 69 [name] => Ist Term - LKG [feetype_id] => 1 [code] => ISEMTUTION [type] => I SEM TUTION FEES [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

stdClass Object ( [id] => 19 [student_session_id] => 278 [fee_session_group_id] => 15 [is_active] => no [created_at] => 2018-04-17 05:18:41 [fee_groups_feetype_id] => 75 [amount] => 2000.00 [due_date] => 2018-04-24 [fee_groups_id] => 69 [name] => Ist Term - LKG [feetype_id] => 2 [code] => IITUTION [type] => II SEM TUTION FEES [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

stdClass Object ( [id] => 19 [student_session_id] => 278 [fee_session_group_id] => 15 [is_active] => no [created_at] => 2018-04-17 05:18:41 [fee_groups_feetype_id] => 76 [amount] => 3000.00 [due_date] => 2018-04-24 [fee_groups_id] => 69 [name] => Ist Term - LKG [feetype_id] => 3 [code] => SPORTS [type] => SPORTS FEES [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

stdClass Object ( [id] => 65 [student_session_id] => 278 [fee_session_group_id] => 16 [is_active] => no [created_at] => 2018-04-17 05:20:20 [fee_groups_feetype_id] => 77 [amount] => 2000.00 [due_date] => 2018-04-24 [fee_groups_id] => 70 [name] => IInd Term - LKG [feetype_id] => 7 [code] => MAGAZINE [type] => MAGAZINE, PHOTOS,BAG, ID etc [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

stdClass Object ( [id] => 65 [student_session_id] => 278 [fee_session_group_id] => 16 [is_active] => no [created_at] => 2018-04-17 05:20:20 [fee_groups_feetype_id] => 78 [amount] => 1500.00 [due_date] => 2018-04-24 [fee_groups_id] => 70 [name] => IInd Term - LKG [feetype_id] => 8 [code] => SCHOLASTICS [type] => CO - SCHOLASTICS [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

stdClass Object ( [id] => 65 [student_session_id] => 278 [fee_session_group_id] => 16 [is_active] => no [created_at] => 2018-04-17 05:20:20 [fee_groups_feetype_id] => 79 [amount] => 1000.00 [due_date] => 2018-04-24 [fee_groups_id] => 70 [name] => IInd Term - LKG [feetype_id] => 9 [code] => BOOKSUNIFORM [type] => BOOKS, NOTEBOOKS, UNIFORM [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 
-1
Maniraj Murugan 17 Apr. 2018 im 13:36

6 Antworten

Beste Antwort

Basierend auf Ihrem bearbeiteten Beitrag mit zusätzlicher foreach-Schleife :

Wenn Sie die Ausgabe in Ihrer letzten foreach-Schleife benötigen, müssen Sie zuerst die Anzahl von $ student_due_fee kennen und mit einem Zähler vergleichen:

stdClass Object
(
    [0] => stdClass Object
        (
            [fees] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [fee_groups_id] => 69
                            [amount] => 1000
                        )

                    [1] => stdClass Object
                        (
                            [fee_groups_id] => 69
                            [amount] => 2000
                        )

                    [2] => stdClass Object
                        (
                            [fee_groups_id] => 70
                            [amount] => 1000
                        )

                )

        )

    [1] => stdClass Object
        (
            [fees] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [fee_groups_id] => 69
                            [amount] => 3000
                        )

                    [1] => stdClass Object
                        (
                            [fee_groups_id] => 70
                            [amount] => 2000
                        )

                    [2] => stdClass Object
                        (
                            [fee_groups_id] => 70
                            [amount] => 1500
                        )

                )

        )

)

Zähle dein Objekt

$count = count((array)$student_due_fee);

Starten Sie Ihren $ Zähler bei 0

$counter = 0;
foreach ($student_due_fee as $fee) {
    foreach ($fee->fees as $fee_value) {

        $group_id = $fee_value->fee_groups_id;

        if(!isset($totals[$group_id])){

            $totals[$group_id] = $fee_value->amount;
        }   else    {

            $totals[$group_id] += $fee_value->amount;
        }
    }
    $counter++;
    if ($counter == $count) {
        echo "\r\n";
        print_r($totals);
    }    
}

AUSGABE:

Array
(
    [69] => 6000
    [70] => 4500
)

Geige

2
Michael Eugene Yuen 17 Apr. 2018 im 16:31

Warum nicht

end($fee->fees)

Damit erhalten Sie das letzte Element in Ihrem Array.

2
ExohJosh 17 Apr. 2018 im 10:40

Sie können die PHP-Standard-Array-Funktion end() wie folgt verwenden

<?php
   $people = array("Peter", "Joe", "Glenn", "Cleveland");
   echo end($people); 
?>

Die Ausgabe des obigen Codes lautet:

Cleveland

Hier ist der Link, über den Sie dieses Ding [hier] auschecken können

Wie von Andreas vorgeschlagen, können wir array_slice() auch verwenden, um dasselbe auf unterschiedliche Weise zu tun.

2
Kishan Oza 17 Apr. 2018 im 11:01

Wie ich in Kommentaren gepostet habe und wie andere in Antworten gepostet haben, funktioniert das Ende gut.
Aber ich verwende gerne array_slice over end, da Sie angeben können, wie viele Artikel, die Sie wollen.

$arr = array(array ( 69 => 3000 ),
array ( 69 => 6000 ),
array ( 69 => 6000, 70 => 3500 ),
array ( 69 => 6000, 70 => 4500 ));

var_dump(array_slice($arr, -1)); // last item
echo "\n\n";
var_dump(array_slice($arr, -2)); // last two items

Eine negative Zahl in array_slice bedeutet, dass sie ab Ende n Anzahl der Elemente zählt.

https://3v4l.org/L4Niu


BEARBEITEN:

$totals = [];

foreach ($fee->fees as $fee_key => $fee_value) {

$group_id = $fee_value->fee_groups_id;

if(!isset($totals[$group_id])){

   $totals[$group_id] = $fee_value->amount;
   }else{

   $totals[$group_id] += $fee_value->amount;

 }

}
print_r($totals);
//var_dump(array_slice($fee->fees, -1));

Wie in den Kommentaren unten erwähnt, handelt es sich bei $ totals um ein Array. Wenn Sie es also nach der Schleife drucken, erhalten Sie die erwartete Ausgabe.

1
Andreas 17 Apr. 2018 im 11:16

Ich würde end() verwenden

Codebeispiel wäre

$test = array('test1', 'test2', 'test3');

echo end($test);

Ausgabe wäre test3

1
Philipp Maurer 17 Apr. 2018 im 11:50

Sie können die Funktion count() verwenden, um die Anzahl der Array-Werte in Ihrem Fall '4' zu ermitteln.

$arrCount = count($array); # will equal 4 for OP
$arrCount = $arrCount - 1;
echo $array[$arrCount]; # last value in the array
0
ArcherBoy27 17 Apr. 2018 im 10:43