php array_column 函数实现

2015-12-15 18:36  3584人阅读  评论 (0)
Tags: php

php array_column 函数实现

php的array_column函数很好用,但是低版本的没有这个函数,抓狂,只能自己实现一个了

if (!function_exists("array_column")) {
    function array_column(array &$rows, $column_key, $index_key = null) {
        $data = array();
        if (empty($index_key)) {
            foreach ($rows as $row) {
                $data[] = $row[$column_key];
            }
        } else {
            foreach ($rows as $row) {
                $data[$row[$index_key]] = $row[$column_key];
            }
        }
        return $data;
    }
}
豫ICP备09035262号-1