"Less" value is removed

Asked

Viewed 30 times

1

Hello, I made an application to run locally in PHP, but I want to give an increment, so that it is faster. I’m not a programmer, I made this application, looking at some examples on the Internet. I have this piece of code that you pick up after taking 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 would like to know how I take values less than 4 characters, such as position [2] => 2, for it does not exist, and position [2] would be 62017 (which is in position 3). I’ve searched a lot of places, and I can’t find anything that could point me in the right direction. remembering that the results are variable, change all the time.

1 answer

0


To withdraw values less than 4 characters can use the function array_filter you are already using by passing a function with the appropriate logic. Since the array values are numerical, filter the 4 houses will match to get only the elements higher than 999.

Example:

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

See this logic being applied in Ideone

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

  • Thanks for the feedback, your modification in array_filter worked, but I didn’t understand what you said in the second part.

  • @now no problem. I was referring specifically to $uasg = array_map('trim', $uasg); that is within the foreach and should move to later in order not to do unnecessary processing. It’s just a small improvement in the code itself. As it is it ends up applying Trim to the same elements several times.

  • It would look something like this: <br/> $numsuasg = $dom->getelementsbytagname('td'); foreach ($numsuasg as $numuasg) { $uasg[] = $numuasg->nodeValue . " n";&#xA; }&#xA; $uasg = array_map('trim', $uasg); // remove os espaços em branco&#xA; $newuasg = preg_replace("/[^0-9]/", "", $uasg); // remove caracteres &#xA; não numericos do output&#xA; $newuasg = array_filter($newuasg, function($elemento){return $elemento > 999; }); $newuasg = array_slice($newuasg, 2); print_r($newuasg);

  • @Johnpablomartins Yes this way, as it is in the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.