PHP如何递归实现json类

时间:2025-10-27 16:52:52 php语言

PHP如何递归实现json类

  PHP如何递归实现json类呢?下面是小编给大家提供的实现方法,大家可以参考阅读,更多详情请关注应届毕业生考试网。

  代码如下:

  <?php

  /*

  * @ anthor:QD

  * @ time:  2013-09-27

  */

  class json{

  private $Arr = array(); /pic/p>

  /pic/p>

  public function json($array)

  {

  if(!is_array($array)) return false;

  $this->Arr = $array;

  }

  /pic/p>

  public function MainArr()

  {

  $arr = $this->Arr;

  if($this->TypeArr($arr))

  {

  $json = $this->NumArr($arr);

  }

  else

  {

  $json = $this->IndexArr($arr);

  }

  return $json;

  }

  /pic/p>

  public function IndexArr($arr)

  {

  $str ="";

  foreach($arr as $k=>$value)

  {

  if(is_array($value))

  {

  if($this->TypeArr($value)) { $sun=$this->NumArr($value);}

  else               {$sun=$this->IndexArr($value);}

  if(strpos($sun,"}") || strpos($sun,"]"))

  {

  $str .= """.$k."":".$sun.",";

  }

  else

  {

  $str .= """.$k."":"".$sun."",";

  }

  }

  else

  {

  $str .= """.$k."":"".$value."",";

  }

  }

  $str = "{".trim($str,",")."}";

  return $str;

  }

  /pic/p>

  public function NumArr($arr)

  {

  $str = "";

  foreach($arr as $value)

  {

  if(is_array($value))

  {

  if($this->TypeArr($value)) { $sun=$this->NumArr($value);}

  else               {$sun=$this->IndexArr($value);}

  if(strpos($sun,"}") || strpos($sun,"]"))

  {

  $str .= $sun.",";

  }

  else

  {

  $str .= """.$sun."",";

  }

  }

  else

  {

  $str .= """.$value."",";

  }

  }

  $str = "[".trim($str,",")."]";

  return $str;

  }

  /pic/p>

  public function TypeArr($arr)

  {

  if(array_values($arr) === $arr) return true;

  return false;

  }

  }

  ?>

【PHP如何递归实现json类】相关文章:

JavaScript如何实现JSON.stringify03-07

PHP递归效率分析03-09

如何实现java汉诺塔递归算法09-05

php如何实现快速排序09-11

PHP中多态如何实现09-12

PHP中Json应用03-01

php实现图片缩放功能类02-04

如何实现PHP图片裁剪与缩放01-24

如何用PHP实现找回密码02-03