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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 10, 2012 Share Posted November 10, 2012 (edited) $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. Edited November 10, 2012 by AyKay47 Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2012 Share Posted November 10, 2012 return causes immediate exit from the function. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2012 Share Posted November 10, 2012 (edited) $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); Edited November 10, 2012 by Pikachu2000 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 10, 2012 Share Posted November 10, 2012 (edited) 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'); } ?> Edited November 10, 2012 by Barand Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.