Php 循环内搜索数组
假设我有以下数组Php 循环内搜索数组,php,arrays,loops,logic,Php,Arrays,Loops,Logic,假设我有以下数组$diff a, a, a, a, b, b, b, a, a, b, b, b, a, a, a, b A represents a value inside $diff. B represents an Array inside $diff. 现在,如果一个在其序列中出现两次以上,并且不是一个数组,我必须对其进行计数(而是一个值)。否则,不要理会它 对于上述输入,代码的功能如下 [a] = not an array; 0 [a,a] = not an array; 0 [
$diff
a, a, a, a, b, b, b, a, a, b, b, b, a, a, a, b
A represents a value inside $diff.
B represents an Array inside $diff.
现在,如果一个在其序列中出现两次以上,并且不是一个数组,我必须对其进行计数(而是一个值)。否则,不要理会它
对于上述输入,代码的功能如下
[a] = not an array; 0
[a,a] = not an array; 0
[a,a,a] = not an array; 3
[a,a,a,a] = not an array; 4
[b] = array;
[b,b] = array;
[b,b,b] = array;
[a] = not an array; 0
[a,a] = not an array; 0
[b] = array;
[b,b] = array;
[b,b,b] = array;
[a] = not an array; 0
[a,a] = not an array; 0
[a,a,a] = not an array; 3
[b] = array;
这是我的尝试,但没有成功!,由于值被替换,因此值被更改
<?php
foreach($diff as $key => $val) {
if (!is_array($diff[$key])) { // THIS MEANS THAT THE CURRENT ELEMENT IS NOT AN ARRAY.
if(is_array($diff[$key-1]) ) { //START OF SEQ. IF THE PREVIOUS ELEMENT IS AN ARRAY AND CURRENT ELEMENT IS NOT AN ARRAY.
$SEQ_START=$key;
$n=1;
for($i=0; $i<=count($diff); $i+=1) { // I AM CHECKING HERE IF THE NEXT 3 ELEMENTS are NOT ARRAY, HENCE I CAN INCREMENT IT
if(!is_array($diff[$SEQ_START+$i])) $n+=1;
else $n=0;
}
}
}
}
?>
您只需要一个计数器来计算连续的非数组值。使用每个数组值增加它,并使用每个非数组值重置它:
$seqLength = 0;
foreach ($arr as $index => $value) {
if (is_array($value)) {
$seqLength++;
echo 'array';
} else {
$seqLength = 0;
echo 'not an array';
}
if ($seqLength > 2) {
echo '; '.$seqLength;
} else {
echo '; 0';
}
}
也许:
根据下面@Grexis的评论更新
$diff = array(array(),'a', 'a', 'a', 'a', array(), array(), array(), 'a', 'a', array(), array(), array(), 'a', 'a', 'a', array());
// Counter to hold current sequence total
$count = 0;
// Array to hold results
$counted = array();
// Loop array
foreach ($diff as $key => $val) {
if (is_array($val)) { // If it is an array
if ($count > 2) { // If the counter is more than 2
$counted[(isset($seq)) ? $seq + 1 : 0] = $count; // add it to the array
}
// Reset the counter
$count = 0;
$seq = $key;
} else {
// Increment the counter
$count++;
}
}
// If there is a >2 counter at the end, add one more result
if ($count > 2) {
$counted[(isset($seq)) ? $seq + 1 : 0] = $count;
}
print_r($counted);
// Outputs:
// Array
// (
// [0] => 4
// [12] => 3
// )
// or if you want the total count
$total = array_sum($counted);
echo $total; // 7
我理解计数的逻辑,但您想要的最终结果是什么?连续段的数量(>2)或连续段中的元素数量(>2)?(e.x.如果是前者,答案将是2,如果是后者,答案将是7)在上面的示例中,我需要答案是7。这是不必要的,但是如果在foreach中包含索引,则可以将序列开始的索引存储为以$counted为单位的索引。类似于$counted[$index-$count]=$count第10行的code>和$counted[计数($diff)-$count]=$count代码>第21行不会说谎,你的实现肯定更整洁。。。炫耀:p
0
0
3
4
5
0
0
3
4
5
$diff = array(array(),'a', 'a', 'a', 'a', array(), array(), array(), 'a', 'a', array(), array(), array(), 'a', 'a', 'a', array());
// Counter to hold current sequence total
$count = 0;
// Array to hold results
$counted = array();
// Loop array
foreach ($diff as $key => $val) {
if (is_array($val)) { // If it is an array
if ($count > 2) { // If the counter is more than 2
$counted[(isset($seq)) ? $seq + 1 : 0] = $count; // add it to the array
}
// Reset the counter
$count = 0;
$seq = $key;
} else {
// Increment the counter
$count++;
}
}
// If there is a >2 counter at the end, add one more result
if ($count > 2) {
$counted[(isset($seq)) ? $seq + 1 : 0] = $count;
}
print_r($counted);
// Outputs:
// Array
// (
// [0] => 4
// [12] => 3
// )
// or if you want the total count
$total = array_sum($counted);
echo $total; // 7