當前位置: 首頁>>代碼示例>>PHP>>正文


PHP CSV::toArray方法代碼示例

本文整理匯總了PHP中CSV::toArray方法的典型用法代碼示例。如果您正苦於以下問題:PHP CSV::toArray方法的具體用法?PHP CSV::toArray怎麽用?PHP CSV::toArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在CSV的用法示例。


在下文中一共展示了CSV::toArray方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: actionCreate

 /**
  * Creates a new item
  * 
  * @access public
  * @return void
  */
 public function actionCreate()
 {
     $this->_checkAuth();
     $currentModel = mb_strtolower($_GET['model'], 'UTF-8');
     $return_array = array();
     $return_custom = false;
     switch ($currentModel) {
         // Get an instance of the respective model
         case 'convert':
             $model = new Convert();
             break;
         default:
             $this->_sendResponse(501, sprintf('Mode <b>create</b> is not implemented for model <b>%s</b>', $_GET['model']));
             exit;
     }
     // Try to assign POST values to attributes
     foreach ($_POST as $var => $value) {
         // Does the model have this attribute?
         if ($model->hasAttribute($var)) {
             $model->{$var} = $value;
         } else {
             // No, raise an error
             $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']));
         }
     }
     if ($currentModel == 'convert') {
         $model->user_id = $this->user_id;
         $return_custom = true;
         if ($model->data_encoding != '' && $model->data_encoding != null) {
             $model->data_encoding = trim(mb_strtoupper($model->data_encoding, 'UTF-8'));
             if (!Encoding::EncodingExists($model->data_encoding)) {
                 $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not valid for model <b>%s</b>. Valid values: ' . implode(', ', Encoding::$list), 'data_encoding', $_GET['model']));
             }
         } else {
             $model->data_encoding = null;
         }
         if ($model->data_encoding != null) {
             $model->data = iconv($model->data_encoding, 'UTF-8', $model->data);
         }
         switch (mb_strtolower($model->data_type, 'UTF-8')) {
             case 'json':
                 if ($model->data != '') {
                     $list = json_decode($model->data, true);
                     // check if array is designed as: array( array("###KEY_TO_REPLACE###" => "Replaced value"), array(...))
                     $keys = array_keys($list);
                     $keys_ok = true;
                     for ($i = 0; $i < count($keys); ++$i) {
                         if (!is_numeric($keys[$i])) {
                             $keys_ok = false;
                         }
                     }
                     if ($keys_ok) {
                         $model->data_to_use = base64_encode(serialize($list));
                     } else {
                         $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is invalid json format for model <b>%s</b>. JSON structure should be: [{"###KEY_TO_REPLACE###":"Replaced Value 1","###KEY###":"Key 1"},{"###KEY_TO_REPLACE###":"Replaced Value 2","###KEY###":"Key 2"}]', 'data', $_GET['model']));
                     }
                 } else {
                     $model->data_to_use = base64_encode(serialize(array()));
                 }
                 break;
             case 'csv':
                 $delimiter = ';';
                 $enclosure = '"';
                 if ($model->data_parameters != null && $model->data_parameters != '') {
                     $params = explode('||', $model->data_parameters);
                     foreach ($params as $param) {
                         $param = explode('==', $param);
                         $key = $param[0];
                         $value = isset($param[1]) ? $param[1] : '';
                         switch ($key) {
                             case 'delimiter':
                                 $delimiter = $value;
                                 break;
                             case 'enclosure':
                                 $enclosure = $value;
                                 break;
                             default:
                                 $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is invalid format for model <b>%s</b>. Valid value for CSV is: \'setting1==value||setting2==value\', example: \'delimiter==;||enclosure=="\', default delimiter is: \';\', default enclosure is: \'"\'', 'data_parameters', $_GET['model']));
                                 break;
                         }
                     }
                 }
                 $data = CSV::toArray($model->data, $delimiter, $enclosure);
                 if (count($data) > 0) {
                     if (count($data) > 1) {
                         $header = $data[0];
                         $list = array();
                         for ($i = 1; $i < count($data); ++$i) {
                             $row = array();
                             for ($c = 0; $c < count($header); ++$c) {
                                 $row[$header[$c]] = isset($data[$c]) ? $data[$c] : '';
                             }
                             $list[] = $row;
                         }
//.........這裏部分代碼省略.........
開發者ID:lidijakralj,項目名稱:bober,代碼行數:101,代碼來源:ApiController.php


注:本文中的CSV::toArray方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。