How to pass an array into a function, and return the results with an array

后端 未结 9 1549
你的背包
你的背包 2021-02-05 07:25

So I\'m trying to learn how to pass arrays through a function, so that I can get around PHP\'s inability to return multiple values. Haven\'t been able to get anything to work so

9条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 08:07

    Try

    $waffles = foo($waffles);
    

    Or pass the array by reference, like suggested in the other answers.

    In addition, you can add new elements to an array without writing the index, e.g.

    $waffles = array(1,2,3); // filling on initialization
    

    or

    $waffles = array();
    $waffles[] = 1;
    $waffles[] = 2;
    $waffles[] = 3;
    

    On a sidenote, if you want to sum all values in an array, use array_sum()

提交回复
热议问题