1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務時間:8:30-17:00
      你可能遇到了下面的問題
      關閉右側工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      php把數(shù)據(jù)寫進文件 php數(shù)據(jù)導入

      php怎樣把一個數(shù)組寫入一個文件

      方法一:

      四川網(wǎng)站建設公司創(chuàng)新互聯(lián)建站,四川網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為四川千余家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設要多少錢,請找那個售后服務好的四川做網(wǎng)站的公司定做!

      //將一個測試的數(shù)組寫入一個PHP文件:

      ?php //要寫入PHP文件的數(shù)組 $write_array = array( '1' = 'oneone', '2'

      = 'two', '3' = 'three', '4' = 'four','5' = 'five' );

      //字符串處理 $string_start = "?php\n"; $string_process =

      var_export($write_array, TRUE);$string_end = "\n?"; $string =

      $string_start.$string_process.$string_end; //開始寫入文件

      echofile_put_contents('test_array.php', $string); ?

      這里用到了兩個函數(shù):

      1,var_export():

      ·var_export — 用來輸出或返回一個變量的字符串表示,它和 var_dump() 的區(qū)別是,var_export()

      可以用來返回關于傳遞給該函數(shù)的變量的結構信息,并且其返回的表示是合法的 PHP 代碼如果 “echo

      $string_process;”,則可以看到輸出結果:

      array ( 1 = 'oneone', 2 = 'two', 3 = 'three', 4 = 'four', 5 = 'five', )

      而它就是我們要寫入 test_array.php 文件的內(nèi)容(除去 php 標簽);

      ·var_dump() 函數(shù)用來打印變量的相關信息,它只用來“打印”,而不會返回值,它的原型是 void var_dump(……),我們來 “var_dump($string_process);”,則可以看到輸出結果:

      string(86) "array ( 1 = 'oneone', 2 = 'two', 3 = 'three', 4 = 'four', 5 = 'five', )"

      可以看到輸出的string(86) “…”,再一次說明了 var_export() 返回的是一個字符串。

      2,file_put_contents():

      file_put_contents — 將一個字符串寫入文件,原型是 int file_put_contents ( string

      filename, string data [, int flags [, resource context]]

      ),這里我們只用到了兩個參數(shù),”string filename”:要寫入的文件名;”string data”:字符串數(shù)據(jù);

      此函數(shù)返回寫入到文件內(nèi)數(shù)據(jù)的字節(jié)數(shù),如果我們 “echo file_put_contents(’test_array.php’, $string);”,則會輸出一個整數(shù) :95。

      因為輸出的 array() 占了 86 個字節(jié),還有的 $string_start 和 $string_end 又占了 9 個字節(jié),轉義字符 換行符 在這里只占 1 個字節(jié)。(不知道這樣解釋恰當不恰當,還有望大家多多指正)

      方法二:json_encode()

      我們常見一些網(wǎng)站在做ajax時返回JSON格式的數(shù)據(jù):

      返回的是json格式的數(shù)據(jù)返回的是json格式的數(shù)據(jù)

      這有什么好處那?很顯然前端在接到返回的數(shù)據(jù)時可以直接使用,而不用再用eval_r('(+ returnString +)')或者 $.parseJSON(returnString ) (jQuery的函數(shù))來轉化為js對象,這樣顯然為用戶省電了。。。

      在網(wǎng)上搜索了一下,這個問題在搜索中文信息的時候比較少,一些說是返回json的都是在前端進行的轉化處理,根本不是返回JSON格式,其實返回json相當?shù)暮唵巍?/p>

      原來的數(shù)據(jù)就是JSON格式

      下例來自《鋒利的jQuery》:

      $(function(){

      $('#send').click(function() {

      $.getJSON('', function(data) {

      $('#resText').empty();

      var html = '';

      $.each( data , function(commentIndex, comment) {

      html += 'div class="comment"h6' +

      comment['username'] + ':/h6p class="para"' +

      comment['content'] + '/p/div';

      })

      $('#resText').html(html);

      })

      })

      })

      你需要做的就是將數(shù)據(jù)存儲為格式正確的 .json或者.js 文件。以下為示例所傳送的json格式的數(shù)據(jù)

      [

      {

      "username": "張三",

      "content": "沙發(fā)."

      },

      {

      "username": "李四",

      "content": "板凳."

      },

      {

      "username": "王五",

      "content": "地板."

      }

      ]

      php輸出JSON格式

      那么php如何輸出json格式?php 使用json_encode函數(shù),然后jQuery使用datatype:json 就可以了嘛? 它的輸出如下:

      php 使用json_encode函數(shù),jQuery使用datatype:json的返回類型php 使用json_encode函數(shù),jQuery使用datatype:json的返回類型

      顯然并非所愿。還是字符串,到底怎么實現(xiàn)?其實很簡單,只要在php文件頭部加入以下代碼:

      header('Content-type: text/json');

      這個頭就是告知此文件輸出類型為 json,這種形式我們見的最多的是驗證碼——php輸出驗證圖片,有時php可以輸出css文件,js文件等做一些有趣的事情。好的,我們測試一下吧。查看示例

      示例代碼:

      ?php

      header('Content-type: text/json');

      $fruits = array (

      "fruits" = array("a" = "orange", "b" = "banana", "c" = "apple"),

      "numbers" = array(1, 2, 3, 4, 5, 6),

      "holes" = array("first", 5 = "second", "third")

      );

      echo json_encode($fruits);

      ?

      php將數(shù)據(jù)寫入文件

      使用form表單post數(shù)據(jù)到PHP,然后用file_put_contents($fileName, $data)寫入文件,$fileName是文件名,$data是要寫入的數(shù)據(jù)

      新建一個a.php文件,將下面的復制進去訪問一下,填寫后點擊提交,會生成一個a.txt的文件,里面是你填寫的內(nèi)容

      可能會有一個notice的報錯,不必理會

      ?php

      $data = $_POST['text'];

      $fileName = 'a.txt';

      file_put_contents($fileName, $data);

      ?

      !doctype html

      html

      head

      meta charset="utf-8"

      titletest/title

      /head

      body

      form action="./a.php" method="post"

      textarea name="text" id="" cols="30" rows="10"/textarea

      input type="submit" value="提交"

      /form

      /body

      /html

      php 如何把數(shù)據(jù)庫中的記錄 寫入到word 中

      從數(shù)據(jù)庫中讀取存儲了與用戶有關的資料,然后把這些資料放到一個以用戶ID(userid)命名的文件夾中,再在這個文件夾里創(chuàng)建一個userid.doc文件,用于存放取到的用戶資料,用戶資料包括文字類型、圖片。

      PHP將數(shù)據(jù)寫入txt文件

      //記錄返回值

      ? ? $write_data_a = [

      ? ? ? ? 'html_url'? =? $getUrl,

      ? ? ? ? 'ip'? ? = $this-get_real_ip(),

      ? ? ? ? 'time'? =? date("Y-m-d H:i:s",time()),

      ? ? ? ? 'res'?? = $response

      ? ? ];

      //轉化為JSON

      ? ? $write_data_a = json_encode($write_data_a) . '||' . "\n";

      ? ? $date = date("Y-m-d", time());

      //項目路徑目錄,判斷是否存在,不存在則創(chuàng)建

      ? ? $lujing = "./360_mobile_res_sd";

      ? ? if(!is_dir($lujing)){

      ? ? ? ? mkdir(iconv("UTF-8", "GBK", $lujing),0777,true);

      ? ? }

      //文件,判斷是否存在,不存在則創(chuàng)建

      ? ? $TxtFileName = "./360_mobile_res_sd/" . $date . "_2.txt";

      ? ? //以讀寫方式打寫指定文件,如果文件不存則創(chuàng)建

      ? ? if(file_exists($TxtFileName))

      ? ? {

      //存在,追加寫入內(nèi)容

      ? ? ? ? file_put_contents($TxtFileName, $write_data_a, FILE_APPEND);

      ? ? }

      ? ? else

      ? ? {

      //不存在,創(chuàng)建并寫入

      ? ? ? ? if( ($TxtRes=fopen ($TxtFileName,"w+")) === FALSE){

      ? ? ? ? ? ? exit();

      ? ? ? ? }

      ? ? ? ? if(!fwrite ($TxtRes,$write_data_a)){ //將信息寫入文件

      ? ? ? ? ? ? fclose($TxtRes);

      ? ? ? ? ? ? exit();

      ? ? ? ? }

      ? ? ? ? fclose ($TxtRes); //關閉指針

      ? ? }


      網(wǎng)頁名稱:php把數(shù)據(jù)寫進文件 php數(shù)據(jù)導入
      URL分享:http://www.ef60e0e.cn/article/doppeoj.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        辛集市| 乐昌市| 禹州市| 辽阳县| 永宁县| 四子王旗| 新沂市| 桐梓县| 汪清县| 搜索| 乐清市| 大埔区| 鲜城| 河东区| 西藏| 娄烦县| 怀远县| 抚松县| 庄河市| 黄平县| 通榆县| 朝阳市| 西峡县| 太保市| 德清县| 东莞市| 彰化市| 嫩江县| 蒙阴县| 如东县| 普格县| 长子县| 墨江| 克山县| 信丰县| 垫江县| 东城区| 色达县| 无为县| 齐齐哈尔市| 红桥区|