How to use PHP array iterator

Recently, we are developing a vision screening electronic reporting system product. The function of this product is to automatically extract the refractive examination data obtained during the vision screening process, and combine the data to automatically generate an easy-to-understand and professional electronic report for the convenience of parents. Check it through the official account or H5 link.

To realize this requirement, the first step is to perform OCR on the paper report printed from the optometry equipment. The image recognition interface returns a two-dimensional array. The original image of the report is like this: The data returned by the OCR interface is like
Insert image description here
this

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)
}

The system's requirement is to extract the two numbers after the two numbers, which must be traversed through the above array, and then extract the next two elements when encountering the number. But in foreach, if you mark it, wait until next time It is troublesome to extract the data when it comes in. Can I directly extract the next two strings after encountering the * string? At this time, the concept of iterator appeared in my mind. Maybe I used python or java before. I came across it during development, so I searched for it, and sure enough, PHP also has iterators! ! !

Next, I briefly looked at the examples in the PHP documentation and started working on it. It went smoothly and was completed in 5 minutes. I will post the code below with simple comments to help everyone understand:

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

Please take a look at the code comments for what you need to pay attention to. It is well encapsulated and easy to understand and call.

Guess you like

Origin blog.csdn.net/one_and_only4711/article/details/121124381