简体   繁体   中英

ASP.NET to PHP array conversion

I have an array in asp. Example SampleArray(100,100). This in PHP would be SampleArray[100][100] if I am correct. I am trying to populate this array and this is what I have so far:

$sampleArray = array(array());
$counter1 = 0;
$counter2 = 0;
for($counter1; $counter1 < 100; $counter1++)
{
    for($counter2; $counter2 < 100; $counter2++)
    {
        $sampleArray[$counter1][$counter2] = $counter1 . " , " . $counter2;
    }
    $counter2 = 0;
}
echo("Sample Array size: " . count($sampleArray));

You are not using your for loops in an efficient way. The first element of the for loop is for initializing the counter.

$sampleArray = array();
for($counter1=0; $counter1<100; ++$counter1) {
  $sampleArray[$counter1] = array();
  for($counter2=0; $counter2<100; ++$counter2)
    $sampleArray[$counter1][$counter2] = $counter1.','.$counter2;
}

Now, you will have a 100x100 array.

The MAIN difference is that I declared each element of $sampleArray to be an array instead of using array(array()) which makes one element in the top level that contains an array instead of 100 elements that each contain an array.

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