douna4762 2013-04-02 10:25
浏览 56
已采纳

PHP翻转多维数组不起作用

I come up with this code:

function multiArrayFlip($array)
{
    $arrayCount = count($array);
    if ($arrayCount != count($array, COUNT_RECURSIVE)) 
    {
        foreach($array as $key => $value)
        {
            if (is_array($value))
            {
                $array[$key] = multiArrayFlip($value);
            }
        }
    }
    else
    {
      array_flip($array);
    }   

    return $array;
}

but it doesnt work. It returns unchanged array.

here is the array data sample:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [zip] => 02135
                        [hispanic_percent] => 7.4
                        [white_percent] => 73.1
                        [black_percent] => 4.2
                        [native_american_percent] => 0
                    )

            )

        [1] => Array
            (
                [0] => Array
                    (
                        [zip] => 02135
                        [school_number] => 1
                        [school_name] => ANOTHER COURSE TO COLLEGE
                        [school_address] => 20 WARREN STREET BRIGHTON MA 02135
                        [contact_number] => 617-635-8865
                        [start_grade] => 9TH GRADE
                        [reduced_lunch_students_count] => 8
                        [reduced_lunch_students_percent] => 120
                        [free_or_reduced_lunch_students_count] => 53
                        [free_or_reduced_lunch_students_percent] => 0
                    )
)
)

展开全部

  • 写回答

2条回答 默认 最新

  • dousi4257 2013-04-02 10:37
    关注

    You have to reassign the return value of the array_flip function to your $array variable in order to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?