How to add multiple variable to array in foreach loop

  arrays, laravel, laravel-livewire, php

i have in database array like this ->
{"1":{"id":"12","amount":"1","max_amount":"1","chances":"100"},
"2":{"id":"13","amount":"1","max_amount":"1","chances":"100"}}

and im outputing this like this ->

 $drop = json_decode($enemi->drops, true);
        $drop_item = [];
        foreach ($drop as $id => $drop) {
            $item_id = $drop['id'];
            $random_number = rand(1,100);
            if($drop['chances'] >= $random_number) {
                $amount = rand($drop['amount'],$drop['max_amount']);
                $user->items = json_decode($user->items, true);
                $count = $amount;
                if(empty($user->items[$item_id])){
                    $count = $amount;
                } else {
                    foreach($user->items[$item_id] as $player_item) {
                        $count = $amount+$player_item;
                    }
                }
                $item_player = $user->items;
                $item_player[$item_id] = array('amount' => $count);
                $user->items = json_encode($item_player);             
                $item = Item::findOrFail($item_id);
                $drop_item[] = $item->name;
            }
            $this->emit('drop', $drop_item);
        }

When the random number is => the drop chances then add this item in $dropitem array and this is working but when i have 2 items in array from db like here, its adding only first item to array $drop_item how i can add both this item?

Source: Laravel

Leave a Reply