简体   繁体   中英

store variables in an mutidimensional array?

i tried to store variables which are set in a while loop in a multi dimensional arrays. Afterwarts i want to print the array out.

what i did:

$counter = 0;
while($counter < 10){
    $a = $counter + 10;
    $b = $counter + 5;
    $file_ar[] = array($a,$b);
    $counter++;
}

/* $file_ar[1-10] = "$a","$b" */

$i = 0;
while(isset($file_ar[$i])) {
    $a = $file_ar[$i][0];
    $b = $file_ar[$i][1];

    echo $a.' is not '.$b;
}

When i run this code i will not get anything.

What is the reason for this?

Thank you!

Here is code-

<?php
$counter = 0;
while($counter < 10){
    $a = $counter + 10;
    $b = $counter + 5;
    $file_ar[] = array($a,$b);
    $counter++;
}
/* $file_ar[1-10] = "$a","$b" */

$i = 0;
while(isset($file_ar[$i])) {
    $a = $file_ar[$i][0];
    $b = $file_ar[$i][1];

    echo $a.' is not '.$b;
    $i++;
}

You need to add the index of the array you are adding to or you are just writing over it.

$counter = 0;
while($counter < 10){
    $a = $counter + 10;
    $b = $counter + 5;
    $file_ar[$counter] = array($a,$b);
    $counter++;
}

$i = 0;
while(isset($file_ar[$i])) {
    $a = $file_ar[$i][0];
    $b = $file_ar[$i][1];
    if ($a != $b)
        echo $a.' is not '.$b;
    else
        echo $a.'='.$b;
    $i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM