本文介紹了從 Foreach Loop PHP 創(chuàng)建關(guān)聯(lián)數(shù)組的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
我有這個(gè) foreach 循環(huán):
I have this foreach loop:
foreach($aMbs as $aMemb){
$ignoreArray = array(1,3);
if (!in_array($aMemb['ID'],$ignoreArray)){
$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
}
}
這會(huì)打印出正確的字段,但它們是數(shù)組內(nèi)的數(shù)組.我需要 foreach 循環(huán)來(lái)輸出一個(gè)像這樣的簡(jiǎn)單數(shù)組:
This prints out the right fields but they are arrays inside arrays. I need the foreach loop to output a simple array like this one:
$aMemberships = array('1' => 'Standard', '2' => 'Silver');
我做錯(cuò)了什么?
推薦答案
您需要更改 $aMemberships 分配
You need to change your $aMemberships assignment
$aMemberships[] = $aMemb['Name'];
如果你想要一個(gè)數(shù)組
$aMemberships[$aMemb['ID']] = $aMemb['Name'];
如果你想要地圖.
你正在做的是將一個(gè)數(shù)組附加到一個(gè)數(shù)組中.
What you are doing is appending an array to an array.
這篇關(guān)于從 Foreach Loop PHP 創(chuàng)建關(guān)聯(lián)數(shù)組的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!