How can I add all of my array values together in PHP?

前端 未结 4 1684
遇见更好的自我
遇见更好的自我 2020-11-27 08:46

How can I add all of my array values together in PHP? Is there a function for this?

4条回答
  •  时光说笑
    2020-11-27 09:24

    Let the given array values may contain integer or may not be. It would be better to have check and filter the values.

    $array = array(-5, "  ", 2, NULL, 13, "", 7, "\n", 4, "\t",  -2, "\t",  -8);
    
    // removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
    
    $result = array_filter( $array, 'is_numeric' );
    echo array_sum($result);
    

提交回复
热议问题