"less than" value is removed

1

Hello, I made an application to run locally in PHP, but I'm trying to give an increment, so it's faster. I am not a programmer, I made this application, seeing some examples on the internet. I have this part of a code that you get after picking up some data from a site by CURL:

//Pega os valores da uasg
$numsuasg = $dom->getElementsByTagName('td');
foreach ($numsuasg as $numuasg) {
$uasg[] = $numuasg->nodeValue . "\n";
}
$uasg = array_map('trim', $uasg); // remove os espaços em branco
$newuasg = preg_replace("/[^0-9]/", "", $uasg);//remove caracteres não numericos do output
$newuasg = array_filter($newuasg, function($elemento){
return $elemento > 999;
});
$newuasg = array_slice($newuasg, 2);
print_r($newuasg);

The result returns:

Array ( [0] => 22017 [1] => 160270 [2] => 2 [3] => 62017 [4] => 135026 [5] 
=> 82017 [6] => 150154 [7] => 102017 [8] => 154040 [9] => 102017 [10] => 
925611 [11] => 132017 [12] => 153073 [13] => 162017 [14] => 154419 [15] => 
192017 [16] => 160536 [17] => 61 [18] => 192017 [19] => 158126 [20] => 
212017 [21] => 158658 [22] => 242017 [23] => 153065 [24] => 252017 [25] => 
135023 [26] => 252017 [27] => 926334 [28] => 332017 [29] => 257003 [30] => 
352017 [31] => 153251 [32] => 372017 [33] => 155009 [34] => 392017 [35] => 
765720 [36] => 512017 [37] => 153033 [38] => 552017 [39] => 153165 [40] => 
842017 [41] => 155124 [42] => 842017 [43] => 158516 [44] => 1642017 [45] => 
153080 [46] => 2072017 [47] => 153054 [48] => 4202017 [49] => 150232 [50] => 
5612017 [51] => 943001 [52] => 8852017 [53] => 943001 [54] => 9382017 [55] 
=> 943001 [56] => 10072017 [57] => 943001 )

I'd like to know how to retrieve values smaller than 4 characters, for example in position [2] = > 2, for it does not exist, and position [2] would be 62017 (which is in position 3). I already researched in several places and found nothing that could indicate a way to solve this. remembering that the results are variable, change at all times.

    
asked by anonymous 05.11.2017 / 19:34

1 answer

0

To remove values that are less than 4 characters, you can use the array_filter function you are already using by passing a function with the appropriate logic. Since the array values are numeric filtering the 4 houses will correspond to stay only with elements greater than 999.

Example:

$newuasg = array_filter($newuasg, function($elemento){
    return $elemento > 999;
});

See this logic to be applied in Ideone

You are also applying a mapping of each value to the trim of that same value within foreach , which ends up processing the same elements several times. Instead you should do this mapping only once and then% w / w%

    
05.11.2017 / 21:19