PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   PHP für Fortgeschrittene und Experten (http://www.selfphp.de/forum/forumdisplay.php?f=13)
-   -   while-schleife verdoppelt anzahl?? häh? (http://www.selfphp.de/forum/showthread.php?t=10504)

florianb 14.05.2005 15:06:35

while-schleife verdoppelt anzahl?? häh?
 
Hallo zusammen,

ich habe ein Problem.

Ich erstelle mit einer for-Schleife ein Array.
Darin werden 100!! Datensätze gespeichert.

Dann übergebe ich dieses Array einer Funktion.

In dieser Funktion soll eine while-Schleife das Array in zwei Spalten darstellen, weils immer von links nach rechts.
Also mein Ansatz:

PHP-Code:

$i=0;
while(
$i<count($arrayname))
{
echo 
$array[$i];
$i++
echo 
$array[$i];


So, jetzt der Witz:

ein count($array) gibt eindeutig 100 Datensätze.
Aber die while-Schleife gibt jetzt aus:
Linke Spalte Rechte Spalte
0 1
1 2
2 3
3 4

|Coding 14.05.2005 15:10:33

AW: while-schleife verdoppelt anzahl?? häh?
 
Schau mal, so muesste es gehen:
PHP-Code:

<?php
for($i 0$i count($array); $i += 2)
  echo 
$array[$i] . ' - ' $array[$i 1] . '<br>';
?>


florianb 14.05.2005 16:47:55

AW: while-schleife verdoppelt anzahl?? häh?
 
sorry,
an sowas dachte ich ja auch schon, aber leider nicht erfolgreich!
danke aber für die hilfe

chris17 14.05.2005 16:57:55

AW: while-schleife verdoppelt anzahl?? häh?
 
Hi,

ein etwas anderer Ansatz
PHP-Code:

for ($i 0$i count($array); $i++) {

    echo 
$array[$i];
    
    if (
$i == 1) { // % Modulo, errechnet den Restwert einer Division
        
echo '<br>';
    }



-> http://de.php.net/manual/de/language...arithmetic.php

|Coding 14.05.2005 17:05:58

AW: while-schleife verdoppelt anzahl?? häh?
 
Sorry, kleiner Denkfehler drin. So klappt es wunderbar:
PHP-Code:

<?php
for($i 0$i count($array); $i += 2)
  echo 
$array[$i] . ' - ' $array[$i 1] . '<br>';
?>


florianb 14.05.2005 17:51:03

AW: while-schleife verdoppelt anzahl?? häh?
 
so, ich habs gerade gelöst:

PHP-Code:

<?

for($i=0;$i<101;$i++)
{
    $arraynum[$i] = $i;
}

$n = 0;
while ($n<count($arraynum))
{
    echo $n;
    echo ":";
    echo $arraynum[$n];
    echo ":";
    $n++;
    echo $n;
    echo ":";
    echo $arraynum[$n];
    $n++;
    echo "<br>";   
}

?>

Ich übergeben jetzt einfach nicht mehr der function den array, sondern setze den array in der function auf global. dann kann ich nen bug in der übergabe-routine des interpreters schon mal ausschließen.

|Coding 14.05.2005 18:00:14

AW: while-schleife verdoppelt anzahl?? häh?
 
So gehts auch:
PHP-Code:

<?php
for($i 0$i 101$i += 2)
  echo 
$i . ($i 101 ' - ' . ($i 1) : '')  . '<br>';
?>



Alle Zeitangaben in WEZ +2. Es ist jetzt 17:08:38 Uhr.

Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.