[英]Adding data to an array
我正在嘗試使用while循環向數組添加數據,但它似乎是將數據添加為字符串而不是數組。 循環/數組是我仍在學習的東西,任何幫助都會很棒。
$c = 0;
$numberofcustom = 5;
$defaults = array(
'title' => __('Follow Us!', 'smw'),
'text' => ''
);
while ($c < $numberofcustom) {
$customnumber = $c + 1;
$defaults.=array(
'custom' . $customnumber . 'name' => __('', 'smw'),
'custom' . $customnumber . 'icon' => __('', 'smw'),
'custom' . $customnumber . 'url' => __('', 'smw')
);
$c++;
}
print_r($defaults);
問題似乎是從循環中添加數據,如果我只是執行print_r
,我只是回到“數組”。
任何幫助,將不勝感激。
UPDATE
我決定不需要多維數組,所以我使用了下面的建議並想出了
while( $c < $numberofcustom){
$customnumber = $c+1;
$defaults['custom'.$customnumber.'name'] = __('', 'smw');
$defaults['custom'.$customnumber.'icon'] = __('', 'smw');
$defaults['custom'.$customnumber.'url'] = __('', 'smw');
$c++;
}
你需要使用$arrayname[] = $var
,這是附加新項目的PHP語法。 看到這個頁面 。
$defaults[] =array(
'custom'.$customnumber.'name' => __('', 'smw'),
'custom'.$customnumber.'icon' => __('', 'smw'),
'custom'.$customnumber.'url' => __('', 'smw')
);
不要這樣做:
$defaults.=array(
'custom'.$customnumber.'name' => __('', 'smw'),
'custom'.$customnumber.'icon' => __('', 'smw'),
'custom'.$customnumber.'url' => __('', 'smw')
);
動態數組鍵幾乎與具有動態名稱的變量一樣糟糕。 改為使用另一個數組級別:
$defaults[$customernumber] = array(
'customname' => __('', 'smw'),
'customicon' => __('', 'smw'),
'customurl' => __('', 'smw'),
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.