When I run below code, I am getting this error:
Severity: error --> Exception: Attempt to assign property "lottery_draw_id" on null C:\xampp\htdocs\lsnew\application\controllers\Admin.php 396

winner_list_temp is declared but how come it becomes null?

Code is here:

public function lottery_prize_winners_list() {

    extract($_POST);

    if (isset($drawid)) {

        $prize_list = $this->db_model->get_lottery_prize_winners_list($drawid);

        $currency_abbr = 'USD'; 

        $winner_list_temp = array();

        foreach ($prize_list as $key => $value) {

            foreach ($value as $v_key => $v_value) {

                $i_key = $prize_list[$key]->prize_position;

                if($v_key == 'lottery_draw_id') {

                    $winner_list_temp[$i_key]->lottery_draw_id = $prize_list[$key]->lottery_draw_id;

                }

            }

        }

        $winner_list = array();

        $i_key = 0;

        foreach ($winner_list_temp as $key => $value) {

            $winner_list[$i_key] = $value;

            $i_key++;

        }

        if(count($winner_list) > 0) {

            echo json_encode($winner_list);

            return true;

        } else {

            $this->output->set_output('false');

        }

    } else {

        $this->output->set_output('0');

    }

}

It isn't that $winner_list_temp is null; it is that while it is declared as an array, you haven't populate that array yet. It is the lottery_draw_id member of $winner_list_temp[$i_key] that is null, as there is no actual entry for $winner_list_temp[$i_key] at that point.

At least, that is my impression; I haven't worked in PHP for a while, so I may be misreading this.

commented: Now how do I declare that member to rectify the issue? Can someone help please +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.