將.Net數組解析為字符串到PHP數組

[英]Parsing .Net array like string to PHP array


A shipping service is providing me with this weird text format response, after some research i found it is very close to a .net array and i am trying to convert to PHP so i can do something useful with it.

一個運輸服務正在向我提供這種奇怪的文本格式響應,經過一些研究我發現它非常接近.net數組,我試圖轉換為PHP,所以我可以做一些有用的東西。

My questions:

我的問題:

  1. What is this format?
  2. 這種格式是什么?
  3. Is there any PHP function i could use to parse it?

    有沒有可用於解析它的PHP函數?

    City[0] = "Elghorashi";
    State[0] = "";
    ZipCode[0] = "";
    CCity[0] = "Elghorashi";
    CState[0] = "";
    CZipCode[0] = "";
    City[1] = "Abugibha";
    State[1] = "";
    ZipCode[1] = "";
    CCity[1] = "Abugibha";
    CState[1] = "";
    CZipCode[1] = "";
    

Thanks

謝謝

1 个解决方案

#1


1  

Here's a quick little snippit I whipped up to convert a string that looks like a PHP array into an actual PHP array. To be clear the $newArray variable is where the array is created to.

這是一個快速的小snippit我掀起來將看起來像PHP數組的字符串轉換為實際的PHP數組。要清楚,$ newArray變量是創建數組的位置。

$string = 'City[0] = "Elghorashi"; State[0] = ""; ZipCode[0] = ""; CCity[0] = "Elghorashi"; CState[0] = ""; CZipCode[0] = ""; City[1] = "Abugibha"; State[1] = ""; ZipCode[1] = ""; CCity[1] = "Abugibha"; CState[1] = ""; CZipCode[1] = "";';
$string = substr($string, 0, -1);

$arr = explode('; ', $string);
$newArray = [];
foreach($arr AS $value){
    $keyVals = explode(' = ', $value);
    preg_match_all('/^(.*?)\[([0-9]+)\]$/', $keyVals[0], $matches);
    $value = is_string($keyVals[1]) ? substr($keyVals[1], 1, -1) : $keyVals[1];
    $newArray[$matches[2][0]][$matches[1][0]] = $value;
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/15/72542dfd4ae94d6998f631ae20524a94.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com