I do not want to use a loop (unless there is no other way). I know how to do this using foreach loop and building 2 arrays but I was wondering if built in function exists in PHP.
我不想使用循环(除非没有其他方法)。我知道如何使用foreach循环和构建2个数组,但我想知道PHP中是否存在内置函数。
$arr = array(1 => 0.1, 2 => 0.20, 3 => 0.40, 4 => 0.60);
I want to get the resulting two arrays after the function call:
我想在函数调用后获得结果的两个数组:
$twoArrays = split_arrays($arr, 0.3);
$twoArrays would hold the values:
$ twoArrays将保存值:
array(
0 => array(1 => 0.1, 2 => 0.20),
1 => array(3 => 0.40, 4 => 0.60)
);
Basically I want 1 array to hold all values less than 0.3 and another greater than 0.3.
基本上我想要1个数组来保存小于0.3的所有值和另一个大于0.3的值。
With loop I can do it, is there a PHP built in function?
有了循环,我可以做到,是否有PHP内置函数?
NOTE: I need to keep the keys as it is.
注意:我需要保持按键原样。
1 个解决方案
#1
2
You could use array_filter()
twice:
您可以使用array_filter()两次:
$bottom = array_filter($myArray, function($val){return ($val<=0.3)});
$top = array_filter($myArray, function($val){return ($val>0.3)});
#1
2
You could use array_filter()
twice:
您可以使用array_filter()两次:
$bottom = array_filter($myArray, function($val){return ($val<=0.3)});
$top = array_filter($myArray, function($val){return ($val>0.3)});