简体   繁体   中英

Array from object contain only one result

I'm using Laravel 5.2 and make code like below, why the variable result is different ($mems and $member)

//Topup.php

$service = new \Spektr\Libraries\Service;
$member= $service->calc_bonus(2, 3);
print"Topup Result";
print_r($member);

And this is my Service class

<?php
namespace Spektr\Libraries;

class Service 
{

    public function calc_bonus($mem_id, $prd_id, $mems=null) {

        $bonus =0;  
        if(is_null($mems)) 
            $mems=[];
        $upline_id=\Spektr\Model\Member::whereMemId($mem_id)->value('upline_id');
        if ($upline_id) {

            \Log::info("upline-id: ".$upline_id);
            $this->calc_bonus($upline_id, $prd_id, $mems);
        }
print"service class result :";
print_r($mems);
        return $mems;
    }
}

when I run topup.php, page result will show result from print_r($mems) and print_r($member) like this

service class result :Array
(
    [0] => Array
        (
            [mem_id] => 2
            [price] => 5100
            [bonus] => 0
        )

    [1] => Array
        (
            [mem_id] => 1
            [price] => 5000
            [bonus] => 100
        )

)
service class result :Array
(
    [0] => Array
        (
            [mem_id] => 2
            [price] => 5100
            [bonus] => 0
        )

)
Topup ResultArray
(
    [0] => Array
        (
            [mem_id] => 2
            [price] => 5100
            [bonus] => 0
        )

)

Why the result on Topup.php is contain only one array ? Please help with my code,

I'm hoping the result from topup.php contains all member with member's upline

The structure of database data is like this

mem_id|upline_id|name
1         null    member A
2          1      member B

Thx

$member is a variable in topup.php Declare it as an array

$service = new \Spektr\Libraries\Service();

$member  =  array();

$member= $service->calc_bonus(2, 3);
print"Topup Result";
print_r($member);

Tested your code, worked with above change.

I've come to this solution base on @raphioly-san answer

<?php
namespace Spektr\Libraries;

class Service 
{

    public function calc_bonus($mem_id, $prd_id, $mems=null) {

        $bonus =0;  
        if(is_null($mems)) 
            $mems=[];
        $upline_id=\Spektr\Model\Member::whereMemId($mem_id)->value('upline_id');
        if ($upline_id) {

            \Log::info("upline-id: ".$upline_id);
            $mems= $this->calc_bonus($upline_id, $prd_id, $mems); //<<-- add variable $mems
        }
print"service class result :";
print_r($mems);
        return $mems;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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