2016-09-20 5 views
0

配列/ jsonをデータに変換するのに問題があります。キー/値のペアの 'キー'が親のImと違うので、それを取得する方法が...理想的には私は各病院の名前を取得し、各病院の名前は時間(08:00)要素CSSを使用して2番目の要素の背景色/テキスト色を設定します。php JSONを後のDB挿入の変数にロードする

<?php 

$data = '[{"name":"Anne Arundel Medical Center","time":["","","",""],"color":[false,false,false,false]},{"name":"Baltimore Washington Medical Center","time":["","","",""],"color":[false,false,false,false]},{"name":"Bon Secours Hospital","time":["","","",""],"color":[false,false,false,false]},{"name":"Carroll Hospital Center","time":["","","",""],"color":[false,false,false,false]},{"name":"Franklin Square (MedStar)","time":["01:16","","",""],"color":["background-color:#ffff00;color:#000000;",false,false,false]},{"name":"Good Samaritan Hospital\u00a0(MedStar)","time":["","","",""],"color":[false,false,false,false]},{"name":"Greater Baltimore Medical Center","time":["","","",""],"color":[false,false,false,false]},{"name":"Harbor Hospital (MedStar)","time":["","","",""],"color":[false,false,false,false]},{"name":"Harford Memorial Hospital (UMUCH)","time":["","","",""],"color":[false,false,false,false]},{"name":"Howard County General Hospital (JHM)","time":["","","",""],"color":[false,false,false,false]},{"name":"Johns Hopkins Bayview Medical Center","time":["","","","",""],"color":[false,false,false,false,false]},{"name":"Johns Hopkins Hospital","time":["","13:19","","",""],"color":[false,"background-color:#ff0000;color:#000000;",false,false,false]},{"name":"Johns Hopkins Hospital (Pediatric ED)","time":["","","","",""],"color":[false,false,false,false,false]},{"name":"Mercy Medical Center","time":["","","",""],"color":[false,false,false,false]},{"name":"Midtown (UM)","time":["","","",""],"color":[false,false,false,false]},{"name":"Northwest Hospital","time":["","","",""],"color":[false,false,false,false]},{"name":"R Adams Cowley Shock Trauma Center","time":["","","","","",""],"color":[false,false,false,false,false,false]},{"name":"Sinai Hospital of Baltimore","time":["","","","",""],"color":[false,false,false,false,false]},{"name":"St. Agnes Hospital","time":["","","",""],"color":[false,false,false,false]},{"name":"St. Joseph\u2019s\u00a0 (UM)","time":["","","",""],"color":[false,false,false,false]},{"name":"Union Memorial Hospital\u00a0 (MedStar)","time":["","","",""],"color":[false,false,false,false]},{"name":"University of Maryland Medical Center","time":["","","",""],"color":[false,false,false,false]},{"name":"Upper Chesapeake Medical Center (UMUCH)","time":["","","",""],"color":[false,false,false,false]}]'; 
$json = json_decode($data, true); 


echo '<pre>'; 
echo print_r($json); 
echo '</pre>'; 
//exit; 

foreach($json[?] as $item) { 
    echo 'Name: ' . $item[?][1] . '<br />'; 
    echo 'Time: ' . $item[?][2] . '<br />'; 
    echo 'color: ' . $item[?][3] . '<br />'; 
} 

?> 
+0

json配列が適切なJSONエンコード文字列のように見えません – rak007

+0

@PaulCrovella配列の各エントリにはキーはありません。私の意見ではこれほど使用できません – rak007

+0

タイトルを取得する方法が不思議ですが、この「データ」からブランドと説明を削除します。存在していないような$ json [1]から... – jeroen

答えて

0

{ 
    "" : [ true ], 
    "00:06" : { 
     "background-color:#ff6600;color:#000000;" : true 
    }, 
    "00:11" : { 
     "background-color:#ffff00;color:#000000;" : true 
    }, 
    "00:16" : { 
     "background-color:#ffff00;color:#000000;" : true 
    }, 
    "00:17" : { 
     "background-color:#ff0000;color:#000000;" : true 
    }, 
    "01:17" : { 
     "background-color:#ffff00;color:#000000;" : true 
    }, 
    "01:30" : { 
     "background-color:#ffff00;color:#000000;" : true 
    }, 
    "02:03" : { 
     "background-color:#000000;color:#ffffff;" : true 
    }, 
    "02:37" : { 
     "background-color:#ffff00;color:#000000;" : true 
    }, 
    "03:03" : { 
     "background-color:#ffff00;color:#000000;" : true 
    }, 
    "14:44" : { 
     "background-color:#ff0000;color:#000000;" : true 
    }, 
    "Anne Arundel Medical Center" : [ true ], 
    "Baltimore Washington Medical Center" : [ true ], 
    "Bon Secours Hospital" : [ true ], 
    "Carroll Hospital Center" : [ true ], 
    "Franklin Square (MedStar)" : [ true ], 
    "Good Samaritan Hospital(MedStar)" : [ true ], 
    "Greater Baltimore Medical Center" : [ true ], 
    "Harbor Hospital (MedStar)" : [ true ], 
    "Harford Memorial Hospital (UMUCH)" : [ true ], 
    "Howard County General Hospital (JHM)" : [ true ], 
    "Johns Hopkins Bayview Medical Center" : [ true ], 
    "Johns Hopkins Hospital" : [ true ], 
    "Johns Hopkins Hospital (Pediatric ED)" : [ true ], 
    "Mercy Medical Center" : [ true ], 
    "Midtown (UM)" : [ true ], 
    "Northwest Hospital" : [ true ], 
    "R Adams Cowley Shock Trauma Center" : [ true ], 
    "Sinai Hospital of Baltimore" : [ true ], 
    "St. Agnes Hospital" : [ true ], 
    "St. Josephs (UM)" : [ true ], 
    "Union Memorial Hospital (MedStar)" : [ true ], 
    "University of Maryland Medical Center" : [ true ], 
    "Upper Chesapeake Medical Center (UMUCH)" : [ true ] 
} 

へのあなたのJSONデータの内訳あなたは時間と病院の間に相関がない見ることができるように。

+0

を修正しました。これはどうやって私がこれに来たかを助けることができます:http://stackoverflow.com/questions/39574672/screen-scraping-php-using-simplehtmldom – BarclayVision

+0

Clairity .. ..固定されたJSONデータソース – BarclayVision

関連する問題