I am new to PHP and I'm trying to convert a simple Javascript function into a PHP function. So far I know to only convert the variables, I need help with the sort function and array filter. Thanks for any help!
var closeRoll = function( number ) {
var rollArray = [36, 48, 60, 72];
var closest = rollArray.filter(function(ele){
return ele - number >= 0
}).sort()[0];
return closest;
};
My half conversion:
function closeRoll( $number ) {
$rollArray = Array(36, 48, 60, 72);
$closest = array_filter( $rollArray, function temp( $rollArray ) {
return $rollArray - $number >= 0;
}); //.sort()[0];
return $closest;
};
You're pretty close, the variable scope in PHP is a little different:
function closeRoll( $number ) {
$rollArray = Array(36, 48, 60, 72);
$closest = array_filter( $rollArray, function($rollArray) use ($number) {
return $rollArray - $number >= 0;
}); //.sort()[0];
return $closest;
}
Basically the different is use ($number)
and you don't need to name the temp function.
Scoping does not work quite like JavaScript; you can't simply refer to $number
inside your callback function, because it does not have a scope chain.
You either have to make it global (not recommended but simple):
<?php
function doFilter($element) { // this could still be anonymous, but doesn't have to be
return $element - $GLOBALS['number'] >= 0;
}
function closeRoll( $number ) {
$GLOBALS['number'] = $number;
$rollArray = Array(36, 48, 60, 72);
$closest = array_filter( $rollArray, 'doFilter' );
return $closest;
};
?>
Or you could utilize the use
operator (an elegant solution):
<?php
function closeRoll( $number ) {
$rollArray = Array(36, 48, 60, 72);
$closest = array_filter( $rollArray, function( $element ) use ($number) {
return $element - $number >= 0;
});
return $closest;
};
?>
Try this
function closeRoll($number) {
$rollArray = array(36, 48, 60, 72);
$closest = array_filter($rollArray, function ($rollArray) use ($number) { return $rollArray - $number >= 0; });
if (!empty($closest))
{
sort($closest);
return array_shift($closest);
}
return null;
}
So if I understand the JavaScript correctly, that function returns the closest matching value in rollArray
that is greater than the given number
parameter?
Here is my solution to it:
function closeRoll($number) {
$rollArray = array(36, 48, 60, 72);
$rollArray = array_filter(
$rollArray,
function($value) use ($number) {
return $value - $number >= 0;
}
);
return reset(sort($rollArray));
}
You're on the right path when using array_filter() . However when giving it an anonymous function as a callback you're bumping into the first JavaScript vs PHP problem - scoping.
In PHP functions does not automatically share the scope where they are defined. If you need to use something from the currrent scope you need to bring it with you using the use keyword.
Last I use reset() to pull out the first value of the sorted array, which should be the closest matching one. You can't use $rollArray[0]
because array_filter() preserves keys and thus 0
might no longer exist.
Here is the example to optimize the search, not straight, but using your brains... :)
<?php
function find($number, array $a)
{
$len = count($a);
if ($len == 0)
{
return null;
}
elseif ($len == 1)
{
return array_shift($a);
}
sort($a, SORT_NUMERIC);
$first = array_slice($a, 0, (int) ($len / 2));
$second = array_slice($a, (int) ($len / 2));
// No need to check on isset because of if ($len == 0) elseif ($len == 1)
if ($second[0] == $number)
{
return $number;
}
$index = count($first) - 1;
if ($first[$index] == $number)
{
return $number;
}
return abs($number - $first[$index]) <= abs($number - $second[0]) ? find($number, $first) : find($number, $second);
}
$a = array(10, 99, 51, 201, 999, 500, 555, 618);
$n = 75;
echo find($n, $a);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.