【问题标题】:How to convert this string representation of an array into an actual php array [duplicate]如何将此数组的字符串表示形式转换为实际的php数组[重复]
【发布时间】:2013-08-07 10:42:12
【问题描述】:

这是一个例子:

$array = "[[51.228697283276,0.45897198252166],[51.228697283276,0.59374016480853],[51.312903819352,0.59374016480853],[51.312903819352,0.45897198252166]]";

【问题讨论】:

    标签: php


    【解决方案1】:

    这看起来像 JSON。您可以使用json_decode() 对其进行解码:

    json_decode($array);
    

    【讨论】:

      【解决方案2】:

      使用此代码将此字符串转换为数组

          <?php 
      $string ="[[51.228697283276,0.45897198252166],[51.228697283276,0.59374016480853],[51.312903819352,0.59374016480853],[51.312903819352,0.45897198252166]]";
      $array = explode(',',$string);
      for($i=0;$i<count($array);$i++){
          $array[$i] = trim($array[$i],'[[');
          $array[$i] = trim($array[$i],']]');
      }
      echo '<pre>'; print_r($array); ?>
      

      【讨论】:

      • 那为什么会比json_decode($string)更好?
      猜你喜欢
      • 2013-12-14
      • 2015-12-31
      • 1970-01-01
      • 2020-07-21
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2014-07-07
      相关资源
      最近更新 更多