public static function getColumns(array $array, array $columns, $allRowsMustHaveAllColumns = false)
    {
        // validation
        foreach ($array as $key => $row) {
            if (!is_array($row)) {
                throw new UnexpectedValueException('Array element "' . $key . '" is not an array');
            }
        }
        foreach ($columns as $key => $column) {
            if (!is_string($column) && !is_numeric($column)) {
                throw new InvalidArgumentException('Invalid column type in columns array, index "' . $key . '"');
            }
        }
        if (!is_bool($allRowsMustHaveAllColumns)) {
            throw new InvalidArgumentException('allRowsMustHaveAllColumns flag must be boolean');
        }

        $return = array_fill_keys($columns, array());
        foreach ($array as $key => $row) {
            foreach ($columns as $column) {
                if (isset($row[$column]) || array_key_exists($column, $row)) {
                    $return[$column][$key] = $row[$column];
                } elseif ($allRowsMustHaveAllColumns) {
                    throw new UnexpectedValueException('Row "' . $key . '" is missing column: "' . $column . '"');
                }
            }
        }

        return $return;
    }
public static function getColumns(array $array, array $columns, $allRowsMustHaveAllColumns = false)
    {
        // validation
        foreach ($array as $key => $row) {
            if (!is_array($row)) {
                throw new UnexpectedValueException('Array element "' . $key . '" is not an array');
            }
        }
        foreach ($columns as $key => $column) {
            if (!is_string($column) && !is_numeric($column)) {
                throw new InvalidArgumentException('Invalid column type in columns array, index "' . $key . '"');
            }
        }
        if (!is_bool($allRowsMustHaveAllColumns)) {
            throw new InvalidArgumentException('allRowsMustHaveAllColumns flag must be boolean');
        }

        $return = array_fill_keys($columns, array());
        foreach ($array as $key => $row) {
            foreach ($columns as $column) {
                if (isset($row[$column]) || array_key_exists($column, $row)) {
                    $return[$column][$key] = $row[$column];
                } elseif ($allRowsMustHaveAllColumns) {
                    throw new UnexpectedValueException('Row "' . $key . '" is missing column: "' . $column . '"');
                }
            }
        }

        return $return;
    }
public static function getColumns ( array $ array , array $ columns , $ allRowsMustHaveAllColumns = false ) { // validation foreach ( $ array as $ key => $ row ) { if ( ! is_array ( $ row ) ) { throw new UnexpectedValueException ( 'Array element "' . $ key . '" is not an array' ) ; } } foreach ( $ columns as $ key => $ column ) { if ( ! is_string ( $ column ) && ! is_numeric ( $ column ) ) { throw new InvalidArgumentException ( 'Invalid column type in columns array, index "' . $ key . '"' ) ; } } if ( ! is_bool ( $ allRowsMustHaveAllColumns ) ) { throw new InvalidArgumentException ( 'allRowsMustHaveAllColumns flag must be boolean' ) ; } $ return = array_fill_keys ( $ columns , array ( ) ) ; foreach ( $ array as $ key => $ row ) { foreach ( $ columns as $ column ) { if ( isset ( $ row [ $ column ] ) || array_key_exists ( $ column , $ row ) ) { $ return [ $ column ] [ $ key ] = $ row [ $ column ] ; } elseif ( $ allRowsMustHaveAllColumns ) { throw new UnexpectedValueException ( 'Row "' . $ key . '" is missing column: "' . $ column . '"' ) ; } } } return $ return ; }
Extracts a column from an array

@param array $array
@param array $columns
@param bool  $allRowsMustHaveAllColumns

@return array
@throws UnexpectedValueException
Extracts a column from an array