अकेले सूची() के साथ कुछ उदाहरण, फिर सूची के साथ 2 उदाहरण() विस्फोट() के साथ संयुक्त।
PHP manual page for list() पर उदाहरण विशेष रूप से रोशन कर रहे हैं:
मूल रूप से, अपनी सूची जब तक चाहें, के रूप में हो सकता है, लेकिन यह एक पूर्ण सूची है। दूसरे शब्दों में, सरणी में वस्तुओं का क्रम स्पष्ट रूप से मायने रखता है, और चीजों को छोड़ने के लिए, आपको अपनी सूची में खाली स्पॉट खाली छोड़ना होगा()।
अंत में, आप स्ट्रिंग सूचीबद्ध नहीं कर सकते हैं।
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";
// Or let's skip to only the third one
list(, , $power) = $info;
echo "I need $power!\n";
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>
उदाहरण के द्वारा कुछ मामलों:
बेकार रिश्तों को list()
, explode()
और Arrays
आवेदन:
<?php
// What they say.
list($firstVar, $secondVar, , , $thirdVar) = explode(' ', 'I love to hate you');
// What you hear.
// Disaplays: I love you
echo "$firstVar $secondVar $thirdVar";
?>
अंत में, आप सरणियों के साथ संयोजन के रूप में सूची() का उपयोग कर सकते हैं। $VARIABLE[]
एक आइटम को एक सरणी में अंतिम स्लॉट में संग्रहीत करता है। आदेश चीजों जमा हो जाती है ध्यान दिया जाना चाहिए, क्योंकि यह शायद आप क्या अपेक्षा के विपरीत है:
<?php
list(, $Var[], ,$Var[] , $Var[]) = explode(' ', 'I love to hate you');
// Displays:
// Array ([0] => you [1] => hate [2] => love)
print_r($Var);
?>
क्यों आदेश चीजों जमा हो जाती है है में है के रूप में यह सूची पर चेतावनी में दी गई है का विवरण() मैनुअल पेज:
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.