I have a line like this in my code:
list($user_id, $name, $limit, $remaining, $reset) = explode('|', $user);
The last 3 parameters may or may not be there. Is there a function similar to list that will automatically ignore those last parameters if the array is smaller than expected?
If any of the trailing optional substrings are missing in the input string, the corresponding variables should be assigned a null
value.
Just add some spare pipes to the end of the string:
list($user_id, $name, $limit, $remaining, $reset) = explode('|', $user.'||||');
problem solved.
Note: If you're loading arbitrary pipe-delimited data, you might want to use str_getcsv()
function rather than explode()
.