在数组中以特定格式存储值,从.csv文件中检索到PHP中的数组。

时间:2022-07-22 13:32:46

An attribute from a .csv file which looks like:

.csv文件中的一个属性,它看起来如下:

[[-8.585676,41.148522],[-8.585712,41.148639],[-8.585685,41.148855]]

I want to split this data into an 2D array USING PHP, which I want to look like this:

我想用PHP将这些数据分割成一个二维数组,我想这样:

arr[0][0] = -8.585676
arr[0][1] = 41.148522

arr[1][0] = -8.585712
arr[1][1] = -41.148639

arr[2][0] = -8.585685
arr[2][1] = 41.148855

I have done it like this:

我这样做过:

$arr = preg_split('[,]', $str);
    $n = count($arr);
    for($i=0;$i<$n;$i++){
        echo $arr[$i];

        echo "<br>";
    }
    echo "<br><br>";

Its output looks like:

它的输出:

[[-8.585676
41.148522]
[-8.585712
41.148639]
[-8.585685
41.148855]

1 个解决方案

#1


0  

This just happens to be a perfect match for a json_decode() call. Simply use $arr = json_decode($str); and it'll do the trick.

这恰好是json_decode()调用的完美匹配。只需使用$arr = json_decode($str);这样就可以了。

#1


0  

This just happens to be a perfect match for a json_decode() call. Simply use $arr = json_decode($str); and it'll do the trick.

这恰好是json_decode()调用的完美匹配。只需使用$arr = json_decode($str);这样就可以了。