Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP list() function is not work. List return value variables values is null why?

$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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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. **

>Solution :

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()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading