$transaction value:
[
'date' => $date,
'check' => $check,
'description' => $description,
'amount' => $amount,
];
My create function:
public function create(array $transaction) : int {
[$date, $check, $description, $amount] = $transaction;
echo '<pre>';
var_dump($date, $check, $description, $amount);
print_r($transaction);
die();
}
result:
Warning: Undefined array key 0 in /var/www/app/models/Transaction.php on line 11
Warning: Undefined array key 1 in /var/www/app/models/Transaction.php on line 11
Warning: Undefined array key 2 in /var/www/app/models/Transaction.php on line 11
Warning: Undefined array key 3 in /var/www/app/models/Transaction.php on line 11
NULL
NULL
NULL
NULL
Array
(
[date] => 01/04/2021
[check] => 7777
[description] => Transaction 1
[amount] => 150.43
)
** Why date, check, description and amount variables is null? ** ** How can i solve this problem, my variables are coming empty. **
CodePudding user response:
If you assign to an array, then your source must have numeric keys, starting with 0, then 1, then 2, etc.
You could use:
[$date, $check, $description, $amount] = array_values($transaction);
but make sure the source items are always in the correct order.
See: array_values()