簡體   English   中英

從具有另一個數組索引的數組中獲取值

[英]get value from array with another array index

我是php編程的新手,非常抱歉任何錯誤。

我有一個$ monthsKeys變量,它包含一個從0到12的數組。我還有另一個數組($ Months),它包含從12月到1月的12個月。

我正在嘗試創建一個將數組作為參數並從$ monthsKeys獲取鍵值的函數,以便它可以返回$ Months中包含的月份名稱。

function getnomMois($monthsKeys){
    $monthName= array(['Decembre','Novembre','Octobre','Septembre','Aout','Juillet','Juin','Mai','Avril','Mars','Fevrier','Janvier']);
    foreach($monthsKeys as $monthName){
        $monthName[] = $monthKeys['numMois'];
    }
}

我期望函數返回什么:

而不是12的月數返回12月

我正在嘗試做的是:

我嘗試從$ monthsKeys數組中獲取鍵,並使用它從$ MonthName數組中獲取值。

我了解我需要做的是:

我必須從1rst數組中提取鍵值,並使用它來獲取與1rst數組鍵對應的第二個數組的值。

即:monthKey [12]應該允許我返回monthName [12] ='December'。

這是您的代碼(以及您遇到的問題)的說明:

function getnomMois($monthsKeys){
    $monthName= array(['Decembre','Novembre','Octobre','Septembre','Aout','Juillet','Juin','Mai','Avril','Mars','Fevrier','Janvier']);
  //^ here $monthName is array
    foreach($monthsKeys as $monthName){
                         //^ here you change $monthName to string
                         //  becasuse you loop over the arrays and put each value
                         //  inside the $monthName variabl
        $monthName[] = $monthKeys['numMois'];
                //^ here are you tring to do array operation,
                //  but the $monthName variable currently is a string (and you can't do that)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM