2015-06-12 21:15
浏览 88


This question already has an answer here:

I would like to do certain calculation in a function that takes a constant value against different values. To do that I created an array and a variable and also a loop that will take each member of the array and the constant value and pass them to the function that does the calculation. I would like to get an array and then I can do more calculations subsequently.

function subtract($a, $b){
    return $c. ',';
$numbers = array(12, 11, 6, 9);

foreach ($numbers as $index=>$value) {
    $deductions=array(subtract($r, $value));
    if (is_array($deductions)){
    //echo $deductions;
        //echo "not array";
//echo $minimum;
echo $deductions;

I get "Array" and not 9,8,3,6 Why is this? Any help is greatly appreciated. echo was partial problem, I get Array ( [0] => 6, ) not 9,8,3,6 as I expected?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongqiu3709 2015-06-12 21:17

    You cannot echo an array. Try using print_r($deductions) or var_dump($deductions).

    Also, the following line is likely incorrect:

    $deductions=array(subtract($r, $value));

    This line will keep replacing the previous $deductions variable so you only end up with one value in your array (6) because 9-3 is 6. If you are wanting to create an array of values you need to add the new value to the array as follows:

    $deductions[] = subtract($r, $value);
    点赞 打赏 评论
  • duanlaiyin2356 2015-06-12 21:17

    You can't echo an array. Use var_dump or print_r instead.

    点赞 打赏 评论
  • dongsu1539 2015-06-12 22:14

    You can also:

     $return += $deduction[$i]." ";
     echo $return;
    点赞 打赏 评论

相关推荐 更多相似问题