Jump to content

Looking Fer Ideas On Searching Array


shamuraq

Recommended Posts

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?

  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);

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');
}
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.