比方说,我有一个多维数组是这样的:计数一定值期多维数组
array(
array("Thing1","OtherThing1"),
array("Thing1","OtherThing2"),
array("Thing2","OtherThing3")
);
我怎么能算多少次值“Thing1”的期多维数组中是否存在?
比方说,我有一个多维数组是这样的:计数一定值期多维数组
array(
array("Thing1","OtherThing1"),
array("Thing1","OtherThing2"),
array("Thing2","OtherThing3")
);
我怎么能算多少次值“Thing1”的期多维数组中是否存在?
尝试这种情况:
$arr =array(
array("Thing1","OtherThing1"),
array("Thing1","OtherThing2"),
array("Thing2","OtherThing3")
);
echo "<pre>";
$res = array_count_values(call_user_func_array('array_merge', $arr));
echo $res['Thing1'];
输出:
Array
(
[Thing1] => 2
[OtherThing1] => 1
[OtherThing2] => 1
[Thing2] => 1
[OtherThing3] => 1
)
它给出每一值的发生。即:Thing1
发生2
次。
编辑:根据OP的评论:“你是指哪个数组产生阵列?” - 输入数组。例如,这将是输入数组:array(array(1,1),array(2,1),array(3,2)),我只希望它计算第一个值(1,2,3)不是第二个值(1,1,2) - gdscei 7分钟前
$arr =array(
array("Thing1","OtherThing1"),
array("Thing1","OtherThing2"),
array("Thing2","OtherThing3")
);
$res = array_count_values(array_map(function($a){return $a[0];}, $arr));
echo $res['Thing1'];
$count = 0;
foreach($array as $key => $value)
{
if(in_array("Thing1", $value)) $count++;
}
多维!使用array_walk_recursive或array_map – Waygood 2013-03-25 10:33:48
使用in_array
可以帮助:
$cont = 0;
//for each array inside the multidimensional one
foreach($multidimensional as $m){
if(in_array('Thing1', $m)){
$cont++;
}
}
echo $cont;
你检查输出吗?...它不工作...必须使用$ cont ++ – 2013-03-25 10:48:12
这是一个tpyo。感谢您的更正。 – Alvaro 2013-03-25 10:59:43
您可以使用array_search
的详细信息,请参阅本http://www.php.net/manual/en/function.array-search.php
这个代码是这是样品在php文档样本中
<?php
function recursiveArraySearchAll($haystack, $needle, $index = null)
{
$aIt = new RecursiveArrayIterator($haystack);
$it = new RecursiveIteratorIterator($aIt);
$resultkeys;
while($it->valid()) {
if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND (strpos($it->current(), $needle)!==false)) { //$it->current() == $needle
$resultkeys[]=$aIt->key(); //return $aIt->key();
}
$it->next();
}
return $resultkeys; // return all finding in an array
} ;
?>
如果在干草堆中发现不止一次针,则返回第一个匹配键。要返回所有匹配值的键值,请使用带可选search_value参数的array_keys()
。
搜索并返回密钥(不是密钥S) – Waygood 2013-03-25 10:36:37
@Waygood阅读文档可以返回密钥 – 2013-03-25 10:37:36
@Waygood如果不止一次在干草堆中发现针,则返回第一个匹配键。要返回所有匹配值的键,请改用带可选search_value参数的array_keys()。 – 2013-03-25 10:40:54
function showCount($arr, $needle, $count=0)
{
// Check if $arr is array. Thx to Waygood
if(!is_array($arr)) return false;
foreach($arr as $k=>$v)
{
// if item is array do recursion
if(is_array($v))
{
$count = showCount($v, $needle, $count);
}
elseif($v == $needle){
$count++;
}
}
return $count;
}
试试这个
$arr =array(
array("Thing1","OtherThing1"),
array("Thing1","OtherThing2"),
array("Thing2","OtherThing3")
);
$abc=array_count_values(call_user_func_array('array_merge', $arr));
echo $abc[Thing1];
+1直接使用数组函数,但我有一个问题如何获得特定的“thing1”计数使用此答案只。 – 2013-03-25 10:45:54
$ answers = array_count_values(call_user_func_array('array_merge',$ arr)); echo $ answers ['Thing1'] – Waygood 2013-03-25 10:57:33
@Waygood:是啊...的确,我更新了array_count_values工作的答案 – 2013-03-25 11:24:47