PHP Assign array to variables

后端 未结 3 1833
北荒
北荒 2021-01-03 18:41

I\'m not sure if my memory is wrong, but when I last used PHP (years ago), I vaguely remember doing something like this:

$firstVariable, $secondVariable = ex         


        
3条回答
  •  悲哀的现实
    2021-01-03 18:49

    First a few examples with list() alone, then 2 examples with list() combined with explode().


    The examples on the PHP manual page for list() are especially illuminating:

    Basically, your list can be as long as you want, but it is an absolute list. In other words the order of the items in the array obviously matters, and to skip things, you have to leave the corresponding spots empty in your list().

    Finally, you can't list string.

    
    

    A few cases by example:

    Applying list(), explode() and Arrays to dysfunctional relationships:

    
    

    Finally, you can use list() in conjunction with arrays. $VARIABLE[] stores an item into the last slot in an array. The order things are stored should be noted, since it's probably the reverse of what you expect:

     you [1] => hate [2] => love )
    print_r($Var);
    ?>
    

    The explanation of why the order things are stored in is as it is is given in the warning on the list() manual page:

    list() assigns the values starting with the right-most parameter. If you are 
    using plain variables, you don't have to worry about this. But if you are 
    using arrays with indices you usually expect the order of the indices in 
    the array the same you wrote in the list() from left to right; which it isn't.
    It's assigned in the reverse order.
    

提交回复
热议问题