Jump to content

Array Help Please.


dagnasty

Recommended Posts

[code]
while($row = mysql_fetch_array($result))
{
$search["{$row['searchquery']}"]++;
$total["{$row['Count']}"]++;
}
[/code]

How exactly does this structure the array?

$search[0]; does not print anything, and neither does $search[0][0];

But print_r($search); puts out a result such as this:

Array ( [superman] => 1 [prey] => 1 [pirates] => 1 [stargate] => 1 [lost] => 1 [psp] => 1 [entourage] => 1 [superman returns] => 1 [cars] => 1 [Pirates Of The Caribbean] => 1 )

Can someone please explain how the above code sets up the array? I don't understand it at all.

What should I do to print out a single item in either of the 2 arrays?
Link to comment
https://forums.phpfreaks.com/topic/14779-array-help-please/
Share on other sites

[quote]$search[0]; does not print anything, and neither does $search[0][0];[/quote]
That is because you have no numerical keys for that array. $search['pirates'] holds the value "1". The "++" on the end of your variables (or array keys) is just incrementing it's value by one.
Link to comment
https://forums.phpfreaks.com/topic/14779-array-help-please/#findComment-59026
Share on other sites

[code]<?php
$result = @mysql_query("SELECT searchquery, COUNT(*) AS `Count` FROM tscrapestats GROUP BY searchquery ORDER BY `Count` DESC LIMIT 10");

while($row = mysql_fetch_array($result))
{
$search[$row['searchquery']]++;
$total[$row['Count']]++;
}
?>[/code]
As you are grouping by searchquery, DISTINCT is unnecessary. They will be anyway.

The keys to the $search array are 'superman', 'lost' etc.

eg. These will always be 1 as each occurs once in the results
[code]echo $search['superman'];[/code]

The keys to the $total array are the count values from the query
eg how many had a count of 10?

[code]echo $total[10];[/code]
Link to comment
https://forums.phpfreaks.com/topic/14779-array-help-please/#findComment-59030
Share on other sites

try
[code]<?php
$result = @mysql_query("SELECT searchquery, COUNT(*) AS `Count`
          FROM tscrapestats
          GROUP BY searchquery
          ORDER BY `Count` DESC
          LIMIT 10");

while(list($sq, $c) = mysql_fetch_row($result))
{
echo "$sq : $c <br>";
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/14779-array-help-please/#findComment-59031
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.