Piwik\DataTable::addRowsFromSimpleArray PHP Method

addRowsFromSimpleArray() public method

Row metadata cannot be added with this method.
public addRowsFromSimpleArray ( array $array )
$array array Array with the following structure: array( array( col1_name => valueA, col2_name => valueC, ...), array( col1_name => valueB, col2_name => valueD, ...), )
    public function addRowsFromSimpleArray($array)
    {
        if (count($array) === 0) {
            return;
        }
        $exceptionText = " Data structure returned is not convertible in the requested format." . " Try to call this method with the parameters '&format=original&serialize=1'" . "; you will get the original php data structure serialized." . " The data structure looks like this: \n \$data = %s; ";
        // first pass to see if the array has the structure
        // array(col1_name => val1, col2_name => val2, etc.)
        // with val* that are never arrays (only strings/numbers/bool/etc.)
        // if we detect such a "simple" data structure we convert it to a row with the correct columns' names
        $thisIsNotThatSimple = false;
        foreach ($array as $columnValue) {
            if (is_array($columnValue) || is_object($columnValue)) {
                $thisIsNotThatSimple = true;
                break;
            }
        }
        if ($thisIsNotThatSimple === false) {
            // case when the array is indexed by the default numeric index
            if (array_keys($array) == array_keys(array_fill(0, count($array), true))) {
                foreach ($array as $row) {
                    $this->addRow(new Row(array(Row::COLUMNS => array($row))));
                }
            } else {
                $this->addRow(new Row(array(Row::COLUMNS => $array)));
            }
            // we have converted our simple array to one single row
            // => we exit the method as the job is now finished
            return;
        }
        foreach ($array as $key => $row) {
            // stuff that looks like a line
            if (is_array($row)) {
                /**
                 * We make sure we can convert this PHP array without losing information.
                 * We are able to convert only simple php array (no strings keys, no sub arrays, etc.)
                 *
                 */
                // if the key is a string it means that some information was contained in this key.
                // it cannot be lost during the conversion. Because we are not able to handle properly
                // this key, we throw an explicit exception.
                if (is_string($key)) {
                    // we define an exception we may throw if at one point we notice that we cannot handle the data structure
                    throw new Exception(sprintf($exceptionText, var_export($array, true)));
                }
                // if any of the sub elements of row is an array we cannot handle this data structure...
                foreach ($row as $subRow) {
                    if (is_array($subRow)) {
                        throw new Exception(sprintf($exceptionText, var_export($array, true)));
                    }
                }
                $row = new Row(array(Row::COLUMNS => $row));
            } else {
                $row = new Row(array(Row::COLUMNS => array($key => $row)));
            }
            $this->addRow($row);
        }
    }

Usage Example

Example #1
0
 /**
  * Returns a new DataTable created with data from a 'simple' array.
  *
  * See {@link addRowsFromSimpleArray()}.
  *
  * @param array $array
  * @return \Piwik\DataTable
  */
 public static function makeFromSimpleArray($array)
 {
     $dataTable = new DataTable();
     $dataTable->addRowsFromSimpleArray($array);
     return $dataTable;
 }
All Usage Examples Of Piwik\DataTable::addRowsFromSimpleArray