donglieshe4692 2010-11-19 03:14
浏览 35
已采纳

将数组从javascript传递给PHP时出错

I am trying to pass my array of lat and lng to my PHP script so that the script can save it to an .txt file.

i manage to serialize my array and pass it to the php script... and on the php side, i manage to unserialize it. However, when i write the array to the file, the file only show as "array array array" ... and not the value... suppose to be "(1.3567, 103.124252) (1.3543, 103.436435)".

Below is my sample code (to serialize):

function serialize(mixed_value) 
   { 
    var _getType = function( inp ) {
    var type = typeof inp, match;
    var key;
    if (type == 'object' && !inp) {
     return 'null';
    }
    if (type == "object") {
     if (!inp.constructor) {
      return 'object';
     }
     var cons = inp.constructor.toString();
     match = cons.match(/(\w+)\(/);
     if (match) {
      cons = match[1].toLowerCase();
     }
     var types = ["boolean", "number", "string", "array"];
     for (key in types) {
      if (cons == types[key]) {
       type = types[key];
       break;
      }
     }
    }
    return type;
   };
   var type = _getType(mixed_value);
   var val, ktype = '';

   switch (type) {
    case "function": 
     val = ""; 
     break;
    case "undefined":
     val = "N";
     break;
    case "boolean":
     val = "b:" + (mixed_value ? "1" : "0");
     break;
    case "number":
     val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
     break;
    case "string":
     val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
     break;
    case "array":
    case "object":
     val = "a";
     var count = 0;
     var vals = "";
     var okey;
     var key;
     for (key in mixed_value) {
      ktype = _getType(mixed_value[key]);
      if (ktype == "function") { 
       continue; 
      }

      okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
      vals += serialize(okey) +
        serialize(mixed_value[key]);
      count++;
     }
     val += ":" + count + ":{" + vals + "}";
     break;
   }
   if (type != "object" && type != "array") {
     val += ";";
    }
   return val;
   }

PHP (to unserialize and paste to file):

$data = $_POST['y'];

$arr = unserialize($data); 

$fp=fopen("route.txt","w+");
foreach($arr as $key => $value){
fwrite($fp,$value."\t");
}

THANKS FOR ALL YOUR HELP! :D

  • 写回答

1条回答 默认 最新

  • doulu4534 2010-11-19 03:24
    关注
    1. Reinvent wheels much? :) There's already JSON for expressly this purpose, and there are a ton of libraries out there for en- and decoding JSON in just about any language. PHP comes with json_decode.
    2. When writing to the file, $value itself is an array. When an array is cast to a string (as is necessary for outputting it), it'll be cast to the string "Array". Use join(',', $value) to output all values of the array separated by commas.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站