WCM - Das österreichische Computer Magazin Forenübersicht
 

Zurück   WCM Forum > Rat & Tat > Programmierung

Programmierung Rat & Tat für Programmierer

Microsoft KARRIERECAMPUS

Antwort
 
Themen-Optionen Ansicht
Alt 18.06.2006, 12:39   #1
athropos
Hero
 
Registriert seit: 24.07.2001
Alter: 46
Beiträge: 801


athropos eine Nachricht über Skype™ schicken
Standard PHP foreach

Hi!

Irgendwie steig ich nicht dahinter, wie das mit dem foreach-Befehl bei php funzt

Gegeben ist (in etwa, sind nur Testdaten) folgendes (2dimensionales) Array
Code:
$liste = array(
  	array('NR'=>1,'Name'=>'gandalf','LeistungTyp'=>'wizard', 'Betrag' => '25')
	,array('NR'=>2,'Name'=>'bilbo','LeistungTyp'=>'hobbit', 'Betrag' => '35')
	,array('NR'=>3,'Name'=>'frodo','LeistungTyp'=>'hobbit','Betrag' => '46')
	,array('NR'=>4,'Name'=>'saruman','LeistungTyp'=>'bad dude','Betrag' => '57')
	,array('NR'=>5,'Name'=>'sauron','LeistungTyp'=>'really bad dude', 'Betrag' => '99')
  );
ich möchte nun einerseits das Array Tabellarisch ausgeben, andererseits im Zuge des Durchlaufs die Beträge summieren und am Ende die Summe bilden.

Wie genau mach ich das?

thx & lg,
Athropos
____________________________________
Einstein, Pascal and Newton are playing hide and seek. Einstein is \"it\" so he faces the wall, closes his eyes and counts to π.
Pascal runs off like an idiot but Newton pulls out a piece of chalk and draws a 1m x 1m square on the ground and then stands in it\'s centre.

Einstein\'s done counting and turns around and sees Newton, so he grabs him and screams \"I found Newton!\"

Then Newton smirks and says, \"I\'m not Newton, I\'m Pascal\".
athropos ist offline   Mit Zitat antworten
Alt 18.06.2006, 12:58   #2
athropos
Hero
 
Registriert seit: 24.07.2001
Alter: 46
Beiträge: 801


athropos eine Nachricht über Skype™ schicken
Standard

oki, ich hab das Problem jetzt darauf reduziert, daß ich nicht weiß, wie ich die Beträge aufsummier..
____________________________________
Einstein, Pascal and Newton are playing hide and seek. Einstein is \"it\" so he faces the wall, closes his eyes and counts to π.
Pascal runs off like an idiot but Newton pulls out a piece of chalk and draws a 1m x 1m square on the ground and then stands in it\'s centre.

Einstein\'s done counting and turns around and sees Newton, so he grabs him and screams \"I found Newton!\"

Then Newton smirks and says, \"I\'m not Newton, I\'m Pascal\".
athropos ist offline   Mit Zitat antworten
Alt 18.06.2006, 12:58   #3
jak
Inventar
 
Registriert seit: 13.06.2001
Beiträge: 1.830


Standard

2 Möglichkeiten (gibt natürlich noch viel mehr):
1.)
Code:
$summe = 0;
foreach ($liste as $ersteDimension){
  echo $ersteDimension['NR']."\t" .$ersteDimension['Name']."\t".$ersteDimension['NR'].$ersteDimension['LeistungsTyp'].$ersteDimension['Betrag']."
\n";
  $summe += $ersteDimension['Betrag']
}
echo $summe;
2.
Code:
$summe = 0;
foreach ($liste as $ersteDimension){
  foreach($ersteDimension as $key => $eintrag){
    echo "$eintrag \t";
    if($key == 'Betrag') $summe += $eintrag;
  }
  echo "
\n";
  echo $ersteDimension['NR']."\t" 
}
echo $summe;
jak
____________________________________
Join the DNRC | Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)
jak ist offline   Mit Zitat antworten
Alt 18.06.2006, 13:47   #4
James
Meisteroptiker
 
Registriert seit: 19.05.2000
Ort: Salzburg
Alter: 43
Beiträge: 1.495


James eine Nachricht über ICQ schicken
Standard

kopier ich einfach mal frech als zitat von http://www.php-resource.de/manual.ph...tion.array-sum

Zitat:
array_sum

(PHP 4 >= 4.0.4, PHP 5)
array_sum -- Liefert die Summe der Werte in einem Array
Beschreibung
number array_sum ( array array )

array_sum() liefert die Summe der Werte eines Arrays als Integer oder Float.

Beispiel 1. array_sum()
<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";

$b = array("a"=>1.2, "b"=>2.3, "c"=>3.4);
echo "sum(b) = " . array_sum($b) . "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
sum(a) = 20
sum(b) = 6.9

Anmerkung: PHP Versionen vor 4.2.1 haben das übergebene Array selbst modifiziert, und Strings in Zahlen konvertiert (welche abhängig von deren Wert meist zu null konvertiert wurden).
James ist offline   Mit Zitat antworten
Alt 18.06.2006, 13:47   #5
athropos
Hero
 
Registriert seit: 24.07.2001
Alter: 46
Beiträge: 801


athropos eine Nachricht über Skype™ schicken
Standard

Hurra!

DANKE @jak
____________________________________
Einstein, Pascal and Newton are playing hide and seek. Einstein is \"it\" so he faces the wall, closes his eyes and counts to π.
Pascal runs off like an idiot but Newton pulls out a piece of chalk and draws a 1m x 1m square on the ground and then stands in it\'s centre.

Einstein\'s done counting and turns around and sees Newton, so he grabs him and screams \"I found Newton!\"

Then Newton smirks and says, \"I\'m not Newton, I\'m Pascal\".
athropos ist offline   Mit Zitat antworten
Antwort


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +2. Es ist jetzt 02:29 Uhr.


Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Forum SEO by Zoints
© 2009 FSL Verlag