PHP တွင်ဇာတိ array_filt_fatch_flat_fatch () function ကို syntax နှင့်အလွန်ထိရောက်သောပုံသေတန်ဖိုးများနှင့်အတူခင်းကျင်းသောတန်ဖိုးများကိုဖြည့်ဆည်းရန်အသုံးပြုနိုင်သည်။
$filled = array_fill(0, 5, 'default');
// ရလဒ်: ['default', 'default', 'default', 'default', 'default']
သို့သော်တစ်ခါတစ်ရံကျွန်ုပ်တို့သည်ဖြည့်ထားသောတန်ဖိုးကိုမသတ်မှတ်နိုင်သော်လည်းယုတ္တိဗေဒအချို့အားဖြင့် dynamically ထုတ်ပေးသည်။ ဥပမာအားဖြင့်, rocportal numeral တန်ဖိုးများ, UUIDs, ကျပန်းနံပါတ်များကိုဖြည့်စွက်ပါ။
ပိုမိုပြောင်းလွယ်ပြင်လွယ်လိုအပ်သောလိုအပ်ချက်များရရှိရန်ကျွန်ုပ်တို့သည်တိုးမြှင့်ထားသော array_filling version () function ကိုဒီဇိုင်းဆွဲနိုင်သည်။ ဤလုပ်ဆောင်ချက်သည်သော့ချက်အမည်ပေါ် မူတည်. သက်ဆိုင်ရာတန်ဖိုးကို အခြေခံ. သက်ဆိုင်ရာတန်ဖိုးကို dynamically value ကို dynamically function function function ကိုပြောင်းလဲရန်ခွင့်ပြုသည်။
မတရားလုံ့လဝီရိယရှိခင်းကျွေးခြင်းတို့ကိုထောက်ပံ့သည်
အညွှန်းကိန်းကိုစိတ်ကြိုက်ပြုပြင်နိုင်သည် (ကိန်းဂဏန်းသို့မဟုတ် string ဖြစ်နိုင်သည်)
တန်ဖိုးကို callback function ကို fixed သို့မဟုတ်ထုတ်လုပ်နိုင်ပါသည်။
callback function ကို parameter သည်လက်ရှိသော့အညွှန်းကိန်းကိုလက်ခံသည်
အောက်ဖော်ပြပါအချက်များသည် array_ adfectipeftefreadefreadity ()) ၏အကောင်အထည်ဖော်မှုဖြစ်သည်။
/**
* array dynamically ဖြည့်ပါ
*
* @param int|string $startIndex သော့ချက်အမည်စတင်ပါ
* @param int $count ဖြည့်အရေအတွက်
* @param mixed $valueOrCallback fixed တန်ဖိုးသို့မဟုတ် callback function ကို
* @return array
*/
function array_fill_advanced($startIndex, int $count, $valueOrCallback): array {
$result = [];
// အညွှန်းကိန်းအညွှန်းကိန်းတစ်ခုကိန်းရှိမရှိဆုံးဖြတ်ပါ
$isNumericIndex = is_int($startIndex);
for ($i = 0; $i < $count; $i++) {
$key = $isNumericIndex ? $startIndex + $i : $startIndex . $i;
if (is_callable($valueOrCallback)) {
$result[$key] = call_user_func($valueOrCallback, $key);
} else {
$result[$key] = $valueOrCallback;
}
}
return $result;
}
$data = array_fill_advanced(1, 3, 'hello');
// ရလဒ်: [1 => 'hello', 2 => 'hello', 3 => 'hello']
$data = array_fill_advanced(0, 5, function($index) {
return bin2hex(random_bytes(4));
});
// ရလဒ်: [0 => '9f3a4b2c', 1 => 'fa92be77', ...]
$data = array_fill_advanced('item_', 3, function($key) {
return strtoupper($key);
});
// ရလဒ်: ['item_0' => 'ITEM_0', 'item_1' => 'ITEM_1', ...]
$data = array_fill_advanced(0, 3, function($index) {
return [
'id' => $index,
'url' => "https://m66.net/resource/{$index}"
];
});
/*
ရလဒ်:
[
0 => ['id' => 0, 'url' => 'https://m66.net/resource/0'],
1 => ['id' => 1, 'url' => 'https://m66.net/resource/1'],
...
]
*/
array_freate_And () PHP developer များကိုယေဘူယျအားဖြင့်ပိုမိုအစွမ်းထက်။ ပြောင်းလွယ်ပြင်လွယ်သောခင်းကျင်းသည့်နည်းလမ်းများဖြင့်ထောက်ပံ့ပေးသည်။ callback လုပ်ဆောင်ချက်များကိုမိတ်ဆက်ပေးခြင်းအားဖြင့်ကျွန်ုပ်တို့သည်ထပ်တူကုဒ်များကိုလျှော့ချရုံသာမကသော့ချက်တန်ဖိုးယုတ္တိဗေဒအပေါ် အခြေခံ. ပြောင်းလဲနေသောအကြောင်းအရာဆောက်လုပ်ရေးကိုအကောင်အထည်ဖော်နိုင်သည်။
၎င်းသည်အသုတ်ဆိုင်ရာအစပိုင်းတွင်အချက်အလက်များကိုလုပ်ဆောင်ခြင်း, စမ်းသပ်မှုနမူနာများကိုထုတ်လုပ်ခြင်းကဲ့သို့သောဖြစ်ရပ်များတွင်အလွန်အသုံးဝင်သည် ။