Mihail Mojsoski Mihail Mojsoski - 3 months ago 14
PHP Question

PHP While Loop for Multidimensional Arrays

So i tried to loop through bunch of arrays using the while loop.
So it does not work like this, how does it work?

while($x<$sQuery($SERVER)['number'])
{
$o = $o . "<Server>
<Name>".$sQuery($SERVER)[$x]['name']."</Name>
<DNS>".$sQuery($SERVER)[$x]['ip']."</DNS>
<Lat>0</Lat>
<Long>0</Long>
<Usage>-1</Usage>
<RankRequired>0</RankRequired>
</Server>"
x++;
}
}


The error is in this line(s):

<Name>".$sQuery($SERVER)[$x]['name']."</Name>
<DNS>".$sQuery($SERVER)[$x]['ip']."</DNS>


The
[$x]
is not allowed and i do not know how i can loop through them, any ideas?

Answer

I would rewrite it this way:

$s = $sQuery($SERVER);

for($i=0; $i<$s['number']; $i++)
{
    $o .= "<Server><Name>".$s[$i]['name']."</Name>
           <DNS>".$s[$i]['ip']."</DNS><Lat>0</Lat><Long>0</Long><Usage>-1</Usage>
           <RankRequired>0</RankRequired></Server>";
}
Comments