Create associative array from Foreach Loop PHP

前端 未结 6 1557
盖世英雄少女心
盖世英雄少女心 2020-12-23 17:43

I have this foreach loop:

foreach($aMbs as $aMemb){
    $ignoreArray = array(1,3);
    if (!in_array($aMemb[\'ID\'],$ignoreArray)){ 
        $aMemberships[]          


        
6条回答
  •  醉梦人生
    2020-12-23 18:21

    Your existing code uses incremental key and uses the array as corresponding value. To make make $aMemberships an associative array with key as $aMemb['ID'] and value being $aMemb['Name'] you need to change

        $aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
    

    in the foreach loop to:

        $aMemberships[$aMemb['ID']] = $aMemb['Name']);
    

提交回复
热议问题