日期:2014-05-17  浏览次数:20578 次

php转js dom格式如何做?
数据库字段 shopinfo 里信息是这样的

绿色@@@/A/11/12/13/@@@/B/21/22/23/###蓝色@@@/A/31/32/33/@@@/B/41/42/43/###绿色@@@/A/51/52/53/@@@/B/61/62/63/

怎么样才能变化成这样的格式呢

<script language="javascript" type="text/javascript">
 var data={
 
  "黑色":[["A","B"],["11","12","13"],["21","22","23"]],
  "红色":[["A","B"],["31","32","33"],["41","42","43"]],
  "绿色":[["A","B"],["51","52","53"],["61","62","63"]],

  };
</script>


------解决方案--------------------
json_encode($arr); //数组转json
------解决方案--------------------
PHP code
$s="黑色@@@/A/11/12/13/@@@/B/21/22/23/###红色@@@/A/31/32/33/@@@/B/41/42/43/###绿色@@@/A/51/52/53/@@@/B/61/62/63/";
foreach(explode('###',$s) as $v){
  $arr = explode('@@@/',$v);
  $k = array_shift($arr);
  $p = $q = array();
  foreach($arr as $r) {
    $t = explode('/', trim($r, '/'));
    $p[] = array_shift($t);
    $q[] = $t;
  }
  $ar[$k] = array_merge(array($p), $q);  
}
print_r($ar);

------解决方案--------------------
中文丢失,是因为不是 utf-8 编码的原因

连接数据库后,执行
mysql_query('set name utf8');
即可在其后取得 utf-8 编码的数据,再做成 json 就没有问题了