PHP array_slice () फ़ंक्शन
उदाहरण
तीसरे सरणी तत्व से टुकड़ा शुरू करें, और शेष तत्वों को सरणी में वापस करें:
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>
परिभाषा और उपयोग
array_slice() फ़ंक्शन किसी सरणी के चयनित भागों को लौटाता है।
नोट: यदि सरणी में स्ट्रिंग कुंजियाँ हैं, तो लौटा हुआ सरणी हमेशा कुंजियों को सुरक्षित रखेगा (उदाहरण 4 देखें)।
वाक्य - विन्यास
array_slice(array, start, length, preserve)
पैरामीटर मान
Parameter | Description |
---|---|
array | Required. Specifies an array |
start | Required. Numeric value. Specifies where the function will start the slice.
0 = the first element.
If this value is set to a negative number, the function will start slicing that far from the last element.
-2 means start at the second last element of the array. |
length | Optional. Numeric value. Specifies the length of the returned array.
If this value is set to a negative number, the function will stop slicing that far from the last element. If this value is not set, the function will return all elements, starting from the position set by the start-parameter.
|
preserve | Optional. Specifies if the function should preserve or reset the keys. Possible values:
|
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | किसी सरणी के चयनित भाग लौटाता है |
---|---|
पीएचपी संस्करण: | 4+ |
पीएचपी चेंजलॉग: | संरक्षित पैरामीटर PHP 5.0.2 . में जोड़ा गया था |
और ज्यादा उदाहरण
उदाहरण 1
दूसरे सरणी तत्व से टुकड़ा शुरू करें, और केवल दो तत्व लौटाएं:
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));
?>
उदाहरण 2
नकारात्मक प्रारंभ पैरामीटर का उपयोग करना:
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,-2,1));
?>
उदाहरण 3
संरक्षित पैरामीटर के साथ सत्य पर सेट:
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2,true));
?>
उदाहरण 4
स्ट्रिंग और पूर्णांक कुंजियों दोनों के साथ:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown");
print_r(array_slice($a,1,2));
$a=array("0"=>"red","1"=>"green","2"=>"blue","3"=>"yellow","4"=>"brown");
print_r(array_slice($a,1,2));
?>
पीएचपी ऐरे संदर्भ