关于javascript:关于PHP数组迭代器的使用方法实例

3次阅读

共计 2660 个字符,预计需要花费 7 分钟才能阅读完成。

在 PHP 的日常操作中, 数组是最常呈现的构造, 而咱们简直每天都在解决数组相干的内容, 这篇文章次要给大家介绍了对于 PHP 数组迭代器的应用办法, 须要的敌人能够参考下

近来在开发一个视力筛查电子报告零碎的产品,这个产品的作用是主动提取视力筛查过程中失去的屈光检查数据,并联合数据主动生成通俗易懂且业余的电子报告,以不便家长能够通过公众号或 H5 链接查阅。

要实现这个需要,第一步是对验光设施里打印进去的纸质报告做 OCR,图片辨认接口返回的是二维数组,报告的原图是这样的:

OCR 接口返回的数据是这样的

array(3) {["words_result"]=>
  array(36) {[0]=>
    array(1) {["words"]=>
      string(8) "FA-6000A"
    }
    [1]=>
    array(1) {["words"]=>
      string(10) "2022-09-16"
    }
    [2]=>
    array(1) {["words"]=>
      string(7) "04:00"
    }
    [3]=>
    array(1) {["words"]=>
      string(8) "SHOP:B"
    }
    [4]=>
    array(1) {["words"]=>
      string(7) "NAME:"
    }
    [5]=>
    array(1) {["words"]=>
      string(3) "<R>"
    }
    [6]=>
    array(1) {["words"]=>
      string(1) "C"
    }
    [7]=>
    array(1) {["words"]=>
      string(1) "A"
    }
    [8]=>
    array(1) {["words"]=>
      string(5) "-1.50"
    }
    [9]=>
    array(1) {["words"]=>
      string(5) "-0.25"
    }
    [10]=>
    array(1) {["words"]=>
      string(3) "131"
    }
    [11]=>
    array(1) {["words"]=>
      string(5) "-1.50"
    }
    [12]=>
    array(1) {["words"]=>
      string(7) "-0,25"
    }
    [13]=>
    array(1) {["words"]=>
      string(3) "122"
    }
    [14]=>
    array(1) {["words"]=>
      string(7) "-1,50"
    }
    [15]=>
    array(1) {["words"]=>
      string(7) "-0,25"
    }
    [16]=>
    array(1) {["words"]=>
      string(3) "114"
    }
    [17]=>
    array(1) {["words"]=>
      string(5) "-1.50"
    }
    [18]=>
    array(1) {["words"]=>
      string(7) "-0,25"
    }
    [19]=>
    array(1) {["words"]=>
      string(3) "122"
    }
    [20]=>
    array(1) {["words"]=>
      string(3) "<L>"
    }
    [21]=>
    array(1) {["words"]=>
      string(1) "C"
    }
    [22]=>
    array(1) {["words"]=>
      string(1) "A"
    }
    [23]=>
    array(1) {["words"]=>
      string(5) "-1.50"
    }
    [24]=>
    array(1) {["words"]=>
      string(4) "+0.0"
    }
    [25]=>
    array(1) {["words"]=>
      string(5) "-1.25"
    }
    [26]=>
    array(1) {["words"]=>
      string(7) "-0,25"
    }
    [27]=>
    array(1) {["words"]=>
      string(3) "158"
    }
    [28]=>
    array(1) {["words"]=>
      string(5) "-1.00"
    }
    [29]=>
    array(1) {["words"]=>
      string(5) "-0.25"
    }
    [30]=>
    array(1) {["words"]=>
      string(3) "100"
    }
    [31]=>
    array(1) {["words"]=>
      string(1) "*"
    }
    [32]=>
    array(1) {["words"]=>
      string(5) "-1.25"
    }
    [33]=>
    array(1) {["words"]=>
      string(4) "+0.0"
    }
    [34]=>
    array(1) {["words"]=>
      string(5) "U0=12"
    }
    [35]=>
    array(1) {["words"]=>
      string(5) "PD=58"
    }
  }
  ["words_result_num"]=>
  int(36)
  ["log_id"]=>
  int(1455742838110100386)
}

而零碎的需要是提取两个号前面的两个数字,那必定是对上述数组做遍历解决,而后遇到号便提取接下来的两个元素,但在 foreach 外面,如果做标记,等下次进来时再提取数据比拟麻烦,能不能在遇到 * 号字符串后,间接提取接下来的两个字符串呢,这时我的脑海里呈现了迭代器的概念,可能是之前用 python 或 java 开发时接触到的吧,于是搜寻了一下,果然 PHP 也是有迭代器的!!!

接下来简略看了一下 PHP 文档中的示例,就开始干了,很顺利,5 分钟竣工,上面把代码贴出来并辅以简略的正文帮忙大家了解:

$usefulNumList = [];
$wordsResult = new \ArrayIterator($wordsResult);// 初始化数组迭代器, 传入数组变量
foreach($wordsResult as $item){$tempWords = $item['words'];
    if(strpos($tempWords, '*') !== false){if($tempWords === '*'){// 有时候,* 号会独自辨认成一个字符串, 有时候会和前面的数字辨认到一起, 如果是独自辨认进去的, 要把指针向后挪一位
            $wordsResult->next();// 实现办法是: 数组变更名 ->next() 办法
        }
       // 留神, 调用了 next() 办法后, 不能再用 $item 去取数组元素值, 要用 current() 办法能力取到 "下一个值"
       array_push($usefulNumList, $this->getCleanNum($wordsResult->current()['words']));
       $wordsResult->next();
       array_push($usefulNumList, $this->getCleanNum($wordsResult->current()['words']));
    }
}

需注意的中央请看一下代码正文,自身封装得很好,很容易了解和调用的。

正文完
 0