简体   繁体   English

从数组中提取负值和正值到两个单独的数组,

[英]Extract negative and positive value from an array to two separate arrays,

I need to divide an array into two arrays. 我需要将一个数组分成两个数组。

One array will contain all positive values, the other all negative values and zero will be considered as a positive value. 一个数组将包含所有正值,另一个数组将包含所有负值,零将被视为正值。

Example array: 示例数组:

$ts = array(7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7);

Without using any array functions.. 不使用任何数组函数..

Pretty straightforward. 非常直截了当。 Just loop through the array and check if the number is less than 0, if so , push it in the negative array else push it in the positive array. 只需遍历数组并检查数字是否小于0,如果是,则将其推入负数组,否则将其推入正数组。

<?php
$ts=array(7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7);
$pos_arr=array(); $neg_arr=array();
foreach($ts as $val)
{
    ($val<0) ?  $neg_arr[]=$val : $pos_arr[]=$val;
}
print_r($pos_arr);
print_r($neg_arr);

OUTPUT :

Array
(
    [0] => 7
    [1] => 13
    [2] => 8
    [3] => 4
    [4] => 3.5
    [5] => 6.5
    [6] => 7
)
Array
(
    [0] => -10
    [1] => -7.2
    [2] => -12
    [3] => -3.7
    [4] => -9.6
    [5] => -1.7
    [6] => -6.2
)

You can use array_filter function, 你可以使用array_filter函数,

$positive = array_filter($ts, function ($v) {
  return $v > 0;
});

$negative = array_filter($ts, function ($v) {
  return $v < 0;
});

Note: This will skip values with 0, or you can just change condition to >=0 in positive numbers filter to considered in positive group. 注意:这将跳过0的值,或者您可以将条件更改为>=0的正数过滤器以在正组中考虑。

DEMO . 演示

Food for thought, you could write a generic function that splits an array based on a boolean result: 值得深思的是,您可以编写一个基于布尔结果拆分数组的泛型函数:

// splits an array based on the return value of the given function
// - add to the first array if the result was 'true'
// - add to the second array if the result was 'false'
function array_split(array $arr, callable $fn)
{
    $a = $b = [];
    foreach ($arr as $key => $value) {
        if ($fn($value, $key)) {
           $a[$key] = $value;
        } else {
           $b[$key] = $value;
        }
    }
    return [$a, $b];
}

list($positive, $negative) = array_split($ts, function($item) {
    return $item >= 0;
});
print_r($positive);
print_r($negative);

Demo 演示

The most elegant is to use phps array_filter() function: 最优雅的是使用array_filter()函数:

<?php

$ts = [ 7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7 ];  

print_r( array_filter( $ts, function( $val ) { return   (0>$val); } ) );
print_r( array_filter( $ts, function( $val ) { return ! (0>$val); } ) );

?>

If you are still using an older php version you need some longer implementation: 如果您仍在使用较旧的PHP版本,则需要更长时间的实现:

<?php

$ts = array( 7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7 );  

print_r( array_filter( $ts, create_function( '$val', 'return   (0>$val);' ) ) );
print_r( array_filter( $ts, create_function( '$val', 'return ! (0>$val);' ) ) );

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM