Exemple #1
0
 /**
  * To CSV conversion
  *
  * @param   mixed   $data
  * @param   mixed   $delimiter
  * @return  string
  */
 public function toCsv($data = null, $delimiter = null)
 {
     // csv format settings
     $newline = Arr::get($this->config, 'csv.newline', "\n");
     $delimiter or $delimiter = Arr::get($this->config, 'csv.delimiter', ',');
     $enclosure = Arr::get($this->config, 'csv.enclosure', '"');
     $escape = Arr::get($this->config, 'csv.escape', '\\');
     // escape function
     $escaper = function ($items) use($enclosure, $escape) {
         return array_map(function ($item) use($enclosure, $escape) {
             return str_replace($enclosure, $escape . $enclosure, $item);
         }, $items);
     };
     if ($data === null) {
         $data = $this->data;
     }
     if (is_object($data) and !$data instanceof \Iterator) {
         $data = $this->toArray($data);
     }
     // Multi-dimensional array
     if (is_array($data) and Arr::isMulti($data)) {
         $data = array_values($data);
         if (Arr::isAssoc($data[0])) {
             $headings = array_keys($data[0]);
         } else {
             $headings = array_shift($data);
         }
     } else {
         $headings = array_keys((array) $data);
         $data = array($data);
     }
     $output = $enclosure . implode($enclosure . $delimiter . $enclosure, $escaper($headings)) . $enclosure . $newline;
     foreach ($data as $row) {
         $output .= $enclosure . implode($enclosure . $delimiter . $enclosure, $escaper((array) $row)) . $enclosure . $newline;
     }
     return rtrim($output, $newline);
 }
Exemple #2
0
 /**
  * Recursively merge two or more arrays. Values in an associative array
  * overwrite previous values with the same key. Values in an indexed array
  * are appended, but only when they do not already exist in the result.
  *
  * Note that this does not work the same as [array_merge_recursive](http://php.net/array_merge_recursive)!
  *
  *     $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane'));
  *     $mary = array('name' => 'mary', 'children' => array('jane'));
  *
  *     // John and Mary are married, merge them together
  *     $john = Arr::merge($john, $mary);
  *
  *     // The output of $john will now be:
  *     array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane'))
  *
  * @param   array  $array1      initial array
  * @param   array  $array2,...  array to merge
  * @return  array
  */
 public static function merge($array1, $array2)
 {
     if (Arr::isAssoc($array2)) {
         foreach ($array2 as $key => $value) {
             if (is_array($value) && isset($array1[$key]) && is_array($array1[$key])) {
                 $array1[$key] = self::merge($array1[$key], $value);
             } else {
                 $array1[$key] = $value;
             }
         }
     } else {
         foreach ($array2 as $value) {
             if (!in_array($value, $array1, true)) {
                 $array1[] = $value;
             }
         }
     }
     if (func_num_args() > 2) {
         foreach (array_slice(func_get_args(), 2) as $array2) {
             if (self::isAssoc($array2)) {
                 foreach ($array2 as $key => $value) {
                     if (is_array($value) && isset($array1[$key]) && is_array($array1[$key])) {
                         $array1[$key] = self::merge($array1[$key], $value);
                     } else {
                         $array1[$key] = $value;
                     }
                 }
             } else {
                 foreach ($array2 as $value) {
                     if (!in_array($value, $array1, true)) {
                         $array1[] = $value;
                     }
                 }
             }
         }
     }
     return $array1;
 }
Exemple #3
0
 /**
  * @param mixed $val
  * @return object
  */
 protected static function recursiveToStdClass($val)
 {
     if (is_array($val)) {
         if (!Arr::isAssoc($val)) {
             return $val;
         }
         return (object) array_map(array('\\CIC\\Cicbase\\Utility\\Arr', 'recursiveToStdClass'), $val);
     } else {
         return $val;
     }
 }