Jump to content

Recommended Posts

$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 by AyKay47

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?

$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 by Pikachu2000

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 by Barand

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.