First, thanks for any help.
首先,感谢您的帮助。
I've spent countless hours on here and other forums trying to find my exact solution but either 1) I'm not understanding the one's I've read or 2)I haven't found the right answer.
我花了无数个小时在这里和其他论坛试图找到我的确切解决方案但是1)我不理解我读过的那个或2)我没有找到正确的答案。
In PHP, I've run a somewhat complex query which returns a set of records similar to:
在PHP中,我运行了一个有点复杂的查询,它返回一组类似于的记录:
id | name | direction|
1 aaa east
2 bbb west
3 ccc east
I've created an associative array such as:
我创建了一个关联数组,例如:
$query=("select * from foo");
$result=mysql_query($query);
$array=mysql_fetch_assoc($result);
Now, what I need to do seems simple but I'm not grasping the concept for some reason. I need to loop through the entire $array and return a count of any value that I want to specify and store that count in a variable.
现在,我需要做的事情似乎很简单,但我并不是出于某些原因而把握这个概念。我需要遍历整个$数组并返回我想指定的任何值的计数,并将该计数存储在变量中。
i.e. Show me how many times east shows up in the "direction" column and put that in a variable called $eastcount
.
即告诉我东方出现在“方向”列中的次数,并将其放入名为$ eastcount的变量中。
I've tried various combinations of using foreach
loops with incremental counts and have tried using array_count_values
but have not been able to put the pieces together :/
我已尝试使用foreach循环和增量计数的各种组合,并尝试使用array_count_values但无法将各个部分组合在一起:/
4 个解决方案
#1
0
// build query
$query=("select * from foo");
// execute query
$result=mysql_query($query);
// declare vars
$east_count = 0;
// iterate through results
while ($data = mysql_fetch_array($result)) {
// grab DIRECTION column value
$direction = $data['direction'];
// detect 'east'
if ($direction == 'east') {
// increment 'east' count
$east_count++;
}
}
// print # of times we had 'east'
echo("direction said 'east' $east_count times");
#2
1
This should work (sorry for the lack of code block I'm on my iPhone).
这应该工作(抱歉我的iPhone上没有代码块)。
http://www.php.net/manual/en/function.array-count-values.php
$array = array(1, "hello", 1, "world", "hello"); print_r(array_count_values($array));
$ array = array(1,“hello”,1,“world”,“hello”);的print_r(array_count_values($阵列));
Array ( [1] => 2 [hello] => 2 [world] => 1 )
数组([1] => 2 [你好] => 2 [世界] => 1)
#3
1
How about this:
这个怎么样:
query=("select * from foo");
$result=mysql_query($query);
$directions = array();
while($direction = mysql_fetch_assoc($result) {
$directions[] = $direction['direction'];
}
$directionCounts = array_count_values($directions);
//now you can access your counts like this:
echo $directionCounts['east'];
#4
0
First, of all you should be using mysqli instead. But, anyhow I hope this makes some sense.
首先,你应该使用mysqli。但是,无论如何,我希望这是有道理的。
if ($result) {
$count = 0;
while ( $row = mysql_fetch_assoc($result)) {
if ($row["route"] === "east") {
$count += 1;
}
}
return $count;
}
#1
0
// build query
$query=("select * from foo");
// execute query
$result=mysql_query($query);
// declare vars
$east_count = 0;
// iterate through results
while ($data = mysql_fetch_array($result)) {
// grab DIRECTION column value
$direction = $data['direction'];
// detect 'east'
if ($direction == 'east') {
// increment 'east' count
$east_count++;
}
}
// print # of times we had 'east'
echo("direction said 'east' $east_count times");
#2
1
This should work (sorry for the lack of code block I'm on my iPhone).
这应该工作(抱歉我的iPhone上没有代码块)。
http://www.php.net/manual/en/function.array-count-values.php
$array = array(1, "hello", 1, "world", "hello"); print_r(array_count_values($array));
$ array = array(1,“hello”,1,“world”,“hello”);的print_r(array_count_values($阵列));
Array ( [1] => 2 [hello] => 2 [world] => 1 )
数组([1] => 2 [你好] => 2 [世界] => 1)
#3
1
How about this:
这个怎么样:
query=("select * from foo");
$result=mysql_query($query);
$directions = array();
while($direction = mysql_fetch_assoc($result) {
$directions[] = $direction['direction'];
}
$directionCounts = array_count_values($directions);
//now you can access your counts like this:
echo $directionCounts['east'];
#4
0
First, of all you should be using mysqli instead. But, anyhow I hope this makes some sense.
首先,你应该使用mysqli。但是,无论如何,我希望这是有道理的。
if ($result) {
$count = 0;
while ( $row = mysql_fetch_assoc($result)) {
if ($row["route"] === "east") {
$count += 1;
}
}
return $count;
}