Get value of array from array key passed as parameter in PHP

时间:2018-02-03 09:05:29

标签: php arrays

I have the following array:

$months = array(
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December');

I have a function to return the name of the month based on $month_id I pass to the function. But the function is always returning January. The function I have is as below:

public function getMonthNameFromID($month_id)
    {
        $months = array(
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December');

        foreach ($months as $m => $m_value) {
            if ($m == $month_id) {
                return $m;
            } else {
                return "0";
            }
        }
    }

I need this function to display January when I pass 1 as input, February when 2 is passed as input etc. But the function is currently display only January as output.

I have edited the function and I am getting the month name based on index I pass to the function. But is there any way I can make the function more useful by checking if index is an allowed value or any suggestions about a better solution.

public function getMonthNameFromID($month_id)
    {
        $months = array(
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December');
        if (!empty($month_id)) {
            for ($i = 1; $i <= 12; $i++) {
                if ($month_id == $i) {
                    return $months[$i];
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

test with this

public function getMonthNameFromID($month_id)
    {
        $months = array(
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December');

        if(isset($months[$month_id])){
            return $months[$month_id];
        }else{
            return 0;
        }
    }

答案 1 :(得分:0)

why don't you rewrite the function as follows

public function getMonthNameFromID($month_id)
    {
        $months = array(
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December');

       return $months[$month_id];
    }