Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace klmn;
- class ArrayTransform
- {
- private $rows;
- public function __construct($rows = array())
- {
- $this->rows = $rows;
- }
- public function _rows_by($index_field_name)
- {
- $output = array();
- foreach ( $this->rows as $row )
- $output[$row[$index_field_name]] = $row;
- return $output;
- }
- public function _rows_by_id()
- {
- return $this->_rows_by('id');
- }
- public function _column($field_name)
- {
- $output = array();
- foreach ( $this->rows as $n => $row )
- $output[$n] = $row[$field_name];
- return $output;
- }
- public function _columns($list = array())
- {
- $output = array();
- foreach ( $this->rows as $n => $row )
- {
- foreach ( $row as $field_name => $value )
- {
- if ( !$list || in_array($field_name, $list) )
- $output[$field_name][$n] = $value;
- }
- }
- return $output;
- }
- public function _cell($field_name)
- {
- if ( $this->rows )
- return $this->rows[0][$field_name];
- }
- public function _cells_by($index_field_name, $value_field_name)
- {
- $output = array();
- foreach ( $this->rows as $row )
- $output[$row[$index_field_name]] = $row[$value_field_name];
- return $output;
- }
- public function _cells_by_id($value_field_name)
- {
- $output = array();
- foreach ( $this->rows as $row )
- $output[$row['id']] = $row[$value_field_name];
- return $output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement