shamuraq Posted November 10, 2012 Share Posted November 10, 2012 Hi guys, I am looking fer a way to delete numbers with decimal values more than 2 decimal place in my arrays. Any pointers? Thanx in advance. Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/ Share on other sites More sharing options...
Manixat Posted November 10, 2012 Share Posted November 10, 2012 More information would lead to a better answer although what you supposedly could do is loop your array and with a simple conditional statement unset the desired values. Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/#findComment-1391587 Share on other sites More sharing options...
AyKay47 Posted November 10, 2012 Share Posted November 10, 2012 $num_arr = array(100.1111, 200.2222, 300.3333); foreach($num_arr as $number) { number_format($number, 2); } this will round every element in the array to the nearest thousandth. Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/#findComment-1391588 Share on other sites More sharing options...
shamuraq Posted November 10, 2012 Author Share Posted November 10, 2012 Its like this:' $value = array( array(3075,15,461.25,16,535.05), array(3075,15,461.25808,16,535.05),//to be removed array(3075,15,461.2,16,535.05234)//to be removed ); $value = array_filter($value, function ($v) { foreach ($v as $x) { if (strlen(substr(strrchr($x, "."), 1)) > 2) return false; } return true; }); var_dump($value); But nothing came out during testing. What could be wrong? Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/#findComment-1391607 Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2012 Share Posted November 10, 2012 return causes immediate exit from the function. Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/#findComment-1391609 Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2012 Share Posted November 10, 2012 On 11/10/2012 at 7:50 PM, AyKay47 said: $num_arr = array(100.1111, 200.2222, 300.3333); foreach($num_arr as $number) { number_format($number, 2); } this will round every element in the array to the nearest thousandth. Not without assigning the value returned from number_format() to anything, it won't. And then it will be to the nearest hundredth. $new_number = number_format($number, 2); Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/#findComment-1391610 Share on other sites More sharing options...
Barand Posted November 10, 2012 Share Posted November 10, 2012 try <?php $value = array( array(3075,15,461.25,16,535.05), array(3075,15,461.25808,16,535.05),//to be removed array(3075,15,461.2,16,535.05234)//to be removed ); function myfilter($v) { return strlen(strrchr("$v", '.')) < 3; } foreach ($value as &$arr) { $arr = array_filter($arr, 'myfilter'); } ?> Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/#findComment-1391612 Share on other sites More sharing options...
DavidAM Posted November 10, 2012 Share Posted November 10, 2012 When the callback function returns false, array_filter will discard the value being processed. In your case, the value being processed is one of your nested arrays; not the scalar value with "too many decimals". You need to run array_filter on each of your nested arrays. Also, review that test condition; I don't think it is doing what you want/think. Link to comment https://forums.phpfreaks.com/topic/270544-looking-fer-ideas-on-searching-array/#findComment-1391613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.