PHP array_slice() လုပ်ဆောင်ချက်
ဥပမာ
တတိယ array ဒြပ်စင်မှ အချပ်ကို စတင်ပြီး array ရှိ ကျန်ဒြပ်စင်များကို ပြန်ပို့ပါ-
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
array_slice() လုပ်ဆောင်ချက်သည် array တစ်ခု၏ ရွေးချယ်ထားသော အစိတ်အပိုင်းများကို ပြန်ပေးသည်။
မှတ်ချက်- array တွင် string keys များရှိပါက၊ ပြန်ပေးထားသော array သည် သော့များကို အမြဲထိန်းသိမ်းထားလိမ့်မည် (ဥပမာ 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:
|
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | array တစ်ခု၏ ရွေးချယ်ထားသော အစိတ်အပိုင်းများကို ပြန်ပေးသည်။ |
---|---|
PHP ဗားရှင်း- | 4+ |
PHP Changelog- | ထိန်းသိမ်းမှု ဘောင်ကို PHP 5.0.2 တွင် ထည့်သွင်းခဲ့သည် ။ |
နောက်ထပ် ဥပမာများ
ဥပမာ ၁
ဒုတိယ array ဒြပ်စင်မှ အချပ်ကို စတင်ပြီး ဒြပ်စင်နှစ်ခုကိုသာ ပြန်ပို့ပါ-
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));
?>
ဥပမာ ၂
အနုတ်လက္ခဏာ စတင်မှုဘောင်ကို အသုံးပြုခြင်း-
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,-2,1));
?>
ဥပမာ ၃
ထိန်းသိမ်းမှု ကန့်သတ်ဘောင်ဖြင့် အမှန်ဟု သတ်မှတ်ထားသည်-
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2,true));
?>
ဥပမာ ၄
စာကြောင်းနှင့် ကိန်းပြည့်သော့ နှစ်ခုလုံးဖြင့်-
<?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));
?>
❮ PHP Array အကိုးအကား