【问题标题】:php searching array inside loopphp在循环内搜索数组
【发布时间】:2012-01-25 23:53:02
【问题描述】:

假设我有以下数组$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 是否在其序列中出现 more than two timesis not an Array (instead a value)。否则,请忽略它。

对于上述输入,代码应如下所示

[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;
            }
        }
    }
}

?>

【问题讨论】:

  • 我理解计数的逻辑,但您要寻找的最终结果是什么?连续段的数量(> 2),还是连续段中的元素数量(> 2)? (例如,如果是前者,答案是 2,如果是后者,答案是 7)
  • 在上面的例子中我需要答案是 7。

标签: php arrays loops logic


【解决方案1】:

已更新根据@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

See it working

【讨论】:

  • 没有必要,但是如果你在foreach中包含索引,你可以将序列开始的索引存储为$counted中的索引。类似于第 10 行的 $counted[$index-$count] = $count; 和第 21 行的 $counted[count($diff)-$count] = $count;
  • 不会撒谎,你的实现肯定更整洁......炫耀:p
【解决方案2】:

您只需要一个计数器来计算连续的非数组值。用每个数组值增加它并用每个非数组值重置它:

$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';
    }
}

【讨论】:

    【解决方案3】:

    也许:

    $a = array(1,1,1,1,1,array(1,1),1,1,1,1,1,array(1,1));
    $i = 0;
    $j = 0;
    foreach ($a as $item){
        if ($i>=2){
            $j=$i + 1;
        }
        if (!is_array($item)){
            $i++;
            echo $j.'<br>';
        }else{
            $i=0;
            $j=0;
            echo '<br>';
        }   
    }
    

    输出:

    0
    0
    3
    4
    5
    
    0
    0
    3
    4
    5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-03
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      相关资源
      最近更新 更多