我有这个阵列,我想要数不断重复值。但它没有给出我想要的东西。我已经用一个例子解释了我的需求是什么。所以如果你对如何了解,请帮助我解决这个问题。
Array
(
[0] => Array
(
[0] => 44
[1] => 1132
)
[1] => Array
(
[0] => 27
[1] => 28
[2] => 32
[3] => 37
[4] => 38
[5] => 114
[6] => 117
[7] => 273
[8] => 1132
)
[2] => Array
(
[0] => 27
[1] => 28
[2] => 32
[3] => 34
[4] => 36
[5] => 37
[6] => 38
[7] => 44
[8] => 114
[9] => 117
[10] => 273
)
[3] => Array
(
[0] => 27
[1] => 28
[2] => 32
[3] => 34
[4] => 36
[5] => 37
[6] => 38
[7] => 44
[8] => 114
[9] => 117
[10] => 273
)
)
我想要的结果如下:
array
(
[44]=>1
[1132]=>2
[27]=>3
[28]=>3
[32]=>3
........
[273]=>2
[1132]=>1
)
我这样做了,但它没有给出我想要的结果..而且它也可以是用户定义的。如果用户输入数字“3”,则应连续重复3次或更多次的值。
$s=array();
for($k=0;$k<count($absentNoArray);$k++)
{
for($p=0;$p<count($absentNoArray[$k]);$p++)
{
$temp=$absentNoArray[$k][$p];
$count=1;
for($l=$k+1;$l<count($absentNoArray);$l++)
{
for($q=0;$q<count($absentNoArray[$l]);$q++)
{
if($temp==$absentNoArray[$l][$q])
{
$count++;
}
else
{
break;
}
}
}
$s[]=$count;
}
}
print_r($s);
提前谢谢..
答案 0 :(得分:1)
试试这个
$myArray = []; //This is your Array
$combinedArr = [];
foreach ($myArray as $item) {
array_push($combinedArr, $item);
}
$quantityArray = array_count_values($combinedArr);
print_r($quantityArray)
答案 1 :(得分:0)
如果你想在数组中直接计算不同的值,对于任何递归级别:
function array_val_counter($input) {
$response = array();
if (is_array($input)) {
array_walk($input,function($value,$key) {
if (is_array($value)) {
$recursiveresponse = array_val_counter($value);
foreach($recursiveresponse as $recurval => $count) {
if (isset($response[$recurval])) {
$response[$recurval]+=$count;
} else {
$response[$recurval]=$count;
}
}
} elseif (isset($response[$value])) {
$response[$value]++;
} else {
$response[$value] = 1;
}
});
} else {
//input is not an array, do some error
}
return $response;
}
编辑:按顺序计算数字:
function array_val_in_seq($input) {
$result = array();
$numarrays = count($input);
for($i=0;$<$numarrays;$i++) {
switch($i) {
case 0:
//for the first array, add every value to the results
//and go through the remaining arrays, stopping if the result is not found
//store how far it got before stopping (aka consecutive count)
foreach($input[0] as $arrayval) {
for($y=1;$y<$numarrays;$y++) {
$result[$arrayval] = 1;
if(in_array($arrayval,$input[$y])) {
$result[$arrayval] = $y+1;
} else {
break;
}
}
}
break;
case $numarrays:
//for the last array, give any elements that do not already exist
// a count of 1
foreach($input[$numarrays] as $arrayval) {
if(array_key_exists($arrayval,$result) === false) {
$result[$arrayval] = 1;
}
}
break;
default:
//if the array value doesn't exist in the previous array (not consecutive) go through the remaining arrays, stopping if the value isn't found.
//if the number is larger than what already exists, replace it.
foreach($input[$i] as $arrayval) {
if(!in_array($arrayval,$input[$i-1])) {
for($y=$i+1;$y<$numarrays;$y++) {
if(in_array($arrayval,$input[$y])) {
$count = $y - $i + 1;
if (!isset($result[$arrayval] ||
(isset($result[$arrayval]) && $result[$arrayval] > $count)) {
$result[$arrayval] =$count;
}
} else {
break;
}
}
}
}
}
}
}
答案 2 :(得分:0)
我不知道这有多好,但它对我有用:
function countConsecutiveValues($arrays, $minCount = 1) {
// Merge all values into a single array
$values = array_unique(call_user_func_array('array_merge', $arrays), SORT_REGULAR);
$counts = array();
// Loop through each value
foreach ($values as $value) {
$count = 0;
$counts[$value] = 0;
foreach ($arrays as $array) {
if (array_search($value, $array)) {
// If the current array contains the value, increment the counter
$count++;
}
else {
// If the current array does not contain the value, update the counts
// array (if necessary) and reset the counter
if ($count >= $minCount) {
$counts[$value] = max($counts[$value], $count);
}
$count = 0;
}
}
// Get the final count after the last array is processed
if ($count >= $minCount) {
$counts[$value] = max($counts[$value], $count);
}
}
// This should remove any zero counts before returning
return array_filter($counts);
}
当像这样使用时:
$arrays = array(
array(44, 1132),
array(27, 28, 32, 37, 38, 114, 117, 273, 1132),
array(27, 28, 32, 34, 36, 37, 38, 44, 114, 117, 273),
array(27, 28, 32, 34, 36, 37, 38, 44, 114, 117, 273)
);
print_r(countConsecutiveValues($arrays, 3));
输出结果为:
Array ( [27] => 3 [28] => 3 [32] => 3 [37] => 3 [38] => 3 [114] => 3 [117] => 3 [273] => 3 )
答案 3 :(得分:0)
我得到了答案,但感谢大家的努力。
$userValue=4;
$idCount=array();
$s=array();
$status=0;
for($k=0;$k<count($absentNoArray);$k++)
{
for($p=0;$p<count($absentNoArray[$k]);$p++)
{
$temp=$absentNoArray[$k][$p];
$count=1;
for($l=$k+1;$l<count($absentNoArray);$l++)
{
foreach($absentNoArray[$l] as $key=>$val)
{
if($temp==$val)
{
$count++;
$status=1;
}
}
if($status==0)
{
break;
}
else
{
if($count<$userValue)
{
$status=0;
continue;
}
else
{
if(!in_array($temp,$s))
{
$s[]=$temp;
break;
}
}
}
}
}
}
print_r($s);