2017-02-10 184 views
-4

我有一個來自API的響應,它是一個字符串中的數組,我想以普通數組格式進行格式化,任何幫助都很明顯。將字符串數組轉換爲普通數組php

「[{ 「CODCHARGE」:30.000, 「DockNo」: 「I31000106219」, 「OrderNo」: 「47735530」, 「原因」: 「訂單 號上傳成功!!」, 「成功」:」是「,」TotalFreight「:125.000}]'

我想從數組中刪除第一個和最後一個qoutes。

format should be :- 
[ 
    {"CODCHARGE":30.000, 
    "DockNo":"I31000106219", 
    "OrderNo":"47735530", 
    "Reason":"Order Nos uploaded Successfully!!", 
    "Succeed":"Yes","TotalFreight":125.000 
    } 
] 
+1

使用'json_decode'功能 – RomanPerekhrest

+1

這不是一個 「字符串數組」 - 這樣的事情根本不存在。那就是'JSON'。 API文檔很可能會告訴你。只需使用'json_decode()' –

回答

1

使用json_decode(),如果你想轉換成數組,二次參數必須是true1

$string = '[{"CODCHARGE":30.000,"DockNo":"I31000106219","OrderNo":"47735530","Reason":"Order Nos uploaded Successfully!!","Succeed":"Yes","TotalFreight":125.000}]'; 
$array = json_decode($string, true); 
print_r($array); 

轉換後:

Array 
(
    [0] => Array 
     (
      [CODCHARGE] => 30 
      [DockNo] => I31000106219 
      [OrderNo] => 47735530 
      [Reason] => Order Nos uploaded Successfully!! 
      [Succeed] => Yes 
      [TotalFreight] => 125 
     ) 

)