2011-10-07 58 views
0

我必須在PHP中解析這個JSON,以便爲每個地址獲取地址,行和城市信息並將其存儲在數據庫中。我打算實施的方式是這樣的:使用有序密鑰後綴的PHP數組遍歷

For each key in the json string, check if it begins with address, 
If yes, split the string based on '_' and get the index count. 
Get line1, line2, city for this index count. 

有沒有更好的方法來做到這一點?

(注意,索引數可以是隨機的)

{ 
    "route": "A", 

    "address_0": "A0", 
    "line1_0": "L1_0", 
    "line2_0": "L2_0", 
    "city_0": "city_0", 

    "address_1": "A1", 
    "line1_1": "L1_1", 
    "line2_1": "L2_1", 
    "city_1": "city_1", 

    "address_2": "A2", 
    "line1_2": "L1_2", 
    "line2_2": "L2_2", 
    "city_2": "city_2", 

} 
+2

的方式諸如PHP的內置[JSON擴展名](HTTP: //php.net/json_decode)? – Wiseguy

回答

0

只需遍歷數據和分配給數組:

$route = array(); 
foreach(json_decode($json) as $p => $v) 
{ 
    list($k, $i) = explode('_', $p, 2) + array(NULL, NULL); 
    if (NULL === $i) { 
     $rc =& $route["$k $v"]; 
     continue; 
    } 
    $rc[$i][$k] = $v; 
} 
unset($rc); 

給出:

array(1) { 
    ["route A"]=> 
    array(3) { 
    [0]=> 
    array(4) { 
     ["address"]=> 
     string(2) "A0" 
     ["line1"]=> 
     string(4) "L1_0" 
     ["line2"]=> 
     string(4) "L2_0" 
     ["city"]=> 
     string(6) "city_0" 
    } 
    [1]=> 
    array(4) { 
     ["address"]=> 
     string(2) "A1" 
     ["line1"]=> 
     string(4) "L1_1" 
     ["line2"]=> 
     string(4) "L2_1" 
     ["city"]=> 
     string(6) "city_1" 
    } 
    [2]=> 
    array(4) { 
     ["address"]=> 
     string(2) "A2" 
     ["line1"]=> 
     string(4) "L1_2" 
     ["line2"]=> 
     string(4) "L2_2" 
     ["city"]=> 
     string(6) "city_2" 
    } 
    } 
} 

Demo

0

是。使用這種格式,而不是

{ 
    "route": "A", 
    data : [{ 
     "address": "A0", 
     "line1": "L1_0", 
     "line2": "L2_0", 
     "city": "city_0" 
    },{ 
     "address": "A1", 
     "line1": "L1_1", 
     "line2": "L2_1", 
     "city": "city_1" 
    },{ 
     "address": "A2", 
     "line1": "L1_2", 
     "line2": "L2_2", 
     "city": "city_2" 
    }] 
} 

,並得到像這樣

$json = ' { 
     "route": "A", 
     data : [{ 
      "address": "A0", 
      "line1": "L1_0", 
      "line2": "L2_0", 
      "city": "city_0" 
     },{ 
      "address": "A1", 
      "line1": "L1_1", 
      "line2": "L2_1", 
      "city": "city_1" 
     },{ 
      "address": "A2", 
      "line1": "L1_2", 
      "line2": "L2_2", 
      "city": "city_2" 
     }] 
    }'; 
$object = json_decode($json); 
echo $object->data[0]->address; // A0 
echo $object->data[1]->address; // A1 
//and so on... 
+0

我不允許更改傳入的JSON,我需要按原樣處理它。 – user330973

+0

正是我在做什麼,但更徹底。 –

+0

@ user330973:哇。所以請聯繫api的所有者,告訴他們必須改變它。因爲這真的很糟糕 – genesis

0

這是一個棘手的,因爲即使是JSON沒有形成很好(address_0 VS ADDRESS_1),而不是像

{ 
    "route" : 
     { "address" : "something", "line1": "something else" }, 
     { ... } 
} 

我會做一個json_decode將你已經成爲一個數組,然後適當地解析鍵,並將值傳遞到DB對象(或循環和保存)。

+0

我認爲你必須處理可憐的JSON的計劃是最好的方法。 –

+0

我正在尋找一種解決方案,我可能不需要兩次遍歷JSON,但看起來像我最終可能會這樣做。 – user330973

0

你總是可以做到這一點:

$json = { 
    "route": "A", 

    "address_0": "A0", 
    "line1_0": "L1_0", 
    "line2_0": "L2_0", 
    "city_0": "city_0", 

    "address_1": "A1", 
    "line1_1": "L1_1", 
    "line2_1": "L2_1", 
    "city_1": "city_1", 

    "address_2": "A2", 
    "line1_2": "L1_2", 
    "line2_2": "L2_2", 
    "city_2": "city_2", 

} 

$object = json_decode($json); 

$route = array(); 
$currentRoute = ""; 
$addressCounter = 0; 

foreach($object as $key => $value) 
{ 
    if($key == "route"){ 
     $currentRoute = $value; 
    } else { 
     $explode = explode("_",$key); 
     $route[$currentRoute][$addressCounter][$explode[0]] = $value; 
     if($explode[0] == "city"){ 
      $addressCounter++; 
     } 
    } 
} 

print_r($route) 

// Should return something like 
// Array (
// ['A'] => Array (
//    [0] => Array (
//      ['address'] => 'A0', 
//      ['line1'] => 'L1_0' 
// ... 

這是討厭的,但它的作品。再說一遍......如果你能控制原來的形式,我會重新思考這種方法,以便在這裏更好地處理。

祝你好運!