Home > Backend Development > PHP Tutorial > 怎么判断每组数组里有多少个正数和负数

怎么判断每组数组里有多少个正数和负数

WBOY
Release: 2016-06-13 13:29:20
Original
1548 people have browsed it

如何判断每组数组里有多少个正数和负数?
$arr=array(array('3','-1','4','2','-2'),array('-3','-1','-4','-12','-2','21','13','-16'),array('-5','-8','4','2'));

------解决方案--------------------

PHP code
$arr=array(
  array('3','-1','4','2','-2'),
  array('-3','-1','-4','-12','-2','21','13','-16'),
  array('-5','-8','4','2')
);
foreach($arr as $k=>$a) {
  $n = count(array_keys(array_map('sign',$a), -1));
  $r[$k] = array('+' => count($a) - $n, '-' => $n);
}

print_r($r);

function sign($n) {
  return $n/abs($n);
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template