Reputation: 123
Bit of a weird title, sorry. I have an array generated from this PHP script which talks to game servers to pull information from them.
A friend told me I should be able to do it through a foreach loop, but I have been trying but not successful. Suck at PHP.
Here is the section of the code I'm stuck on.
try
{
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
$info = $query->getInfo();
foreach ($info $index => $1) {
if ($index == 6 || $index == 7) {
echo $1;
}
/*print_r( $Query->GetInfo( ) );
print_r( $Query->GetPlayers( ) );
print_r( $Query->GetRules( ) ); */
}
catch( Exception $e )
{
echo $e->getMessage( );
}
finally
{
$Query->Disconnect( );
}
?>
Upvotes: 1
Views: 1032
Reputation: 94672
Do you have a $Query = new SourceQuery();
line somewhere?
If this line works
$Query->Connect(....);
Then this line wont
$info = $query->getInfo();
As the first line uses an object called $Query
and the second line uses something called $query
when it should be using the object called $Query
.
Case in this case is very relevant.
Also $info = $query->getInfo();
should be $info = $Query->GetInfo();
This line is also illegal and I would have thought it would throw an error message
foreach ($info $index => $1) {
It is missing the as
syntax and $1
is not legal as variables have to start with an alpha character
It should be something like
foreach ($info as $index => $value) {
if ($index == 6 || $index == 7) {
echo $value;
}
} // you were also missing the closing bracket on your foreach loop
Upvotes: 1
Reputation: 3561
change your foreach syntax
Add as
foreach ($info as $index => $i) {
if ($index == 6 || $index == 7) {
echo $i;
}
Example: Suppose your array is like below
$game = [
0 => 'cricket',
1 => 'baseball',
2 => 'footbal'
];
Then you can get index and value like below.
foreach($game as $index => $value)
{
echo "index is $index and game is $value";
}
It will print
index is 0 and game is cricket
index is 1 and game is baseball
index is 2 and game is football
Hope this will clear your basic foreach concepts
Upvotes: 1