2016-04-05 4 views
-1

最初の2つの配列がjsonファイルの残りの配列のデータに影響を与えるため、ここで正しいことを見つけることに問題があります。 以下の配列のデータにつながるphp double配列を持つparse json

リンクがここにある実際のJSONファイルを表示するには:

http://forecast.weather.gov/MapClick.php?lat=36.3219&lon=-96.8058&FcstType=digitalJSONは、配列を見てみると、「PeriodNumberList」は数に対応し、その中に日のリストがあり、その数は下のアレイに運びますそれは、 "PeriodNameList"配列内のその日の名前を吐き出すためです。

これは難しいところです。ここからの配列の名前は、上の2つの配列から生成されます。ここでは、動的JSONファイルを使用して問題を解析しています。これら2つの下にある配列の名前は常に変化していることを覚えています。なぜ私はそれを解析したのかという推論です。

次の配列を生成し、解析することはできますが、役に立たないようにするために、2つの配列から情報を取得しようとしました。

<?php 

    $url = "http://forecast.weather.gov/MapClick.php? lat=36.3219&lon=-96.8058&FcstType=digitalJSON"; 

// create curl resource 
$ch = curl_init(); 

// set url 
curl_setopt($ch, CURLOPT_URL, $url); 

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 

// $output contains the output string 
$output = curl_exec($ch); 

// close curl resource to free up system resources 
curl_close($ch); 

$json_a = json_decode($output,true); 



// <---------- Period List -------------> // 

    $period0 = $json_a['PeriodNameList']['0']; 

// <---------- Hourly Next 12 ----------> // 

    $hourly_next_12_period = $json_a['$period0']['periodName']; 

    $hourly_next_12_time_0 = $json_a['$period0']['time'][0]; 
    $hourly_next_12_time_1 = $json_a['$period0']['time'][1]; 
    $hourly_next_12_time_2 = $json_a['$period0']['time'][2]; 
    $hourly_next_12_time_3 = $json_a['$period0']['time'][3]; 
    $hourly_next_12_time_4 = $json_a['$period0']['time'][4]; 
    $hourly_next_12_time_5 = $json_a['$period0']['time'][5]; 
    $hourly_next_12_time_6 = $json_a['$period0']['time'][6]; 
    $hourly_next_12_time_7 = $json_a['$period0']['time'][7]; 
    $hourly_next_12_time_8 = $json_a['$period0']['time'][8]; 
    $hourly_next_12_time_9 = $json_a['$period0']['time'][9]; 
    $hourly_next_12_time_10 = $json_a['$period0']['time'][10]; 
    $hourly_next_12_time_11 = $json_a['$period0']['time'][11]; 
    ?> 

ありがとうございました。

+0

トップにある$ urlに注意してください。このポストを作成している間に偶然、URL内のスペースに追加しました。それを無視して、コードを試してみてください。 – Duguayster

+0

JSON配列、多次元配列、PHPで解析するさまざまな方法について、いくつかの研究を行っています。 – Duguayster

+0

私はw3schools、stackoverflow、php.netなどを訪問しましたが、このタイプの配列の例を見つけることはできません。 – Duguayster

答えて

0

私はインターネットを高低で検索した後、ついにそれを理解しました。私は少し時間をかけて、新鮮な視点で戻って来て、自分でそれを理解しました。

奇妙に聞こえるかもしれませんが、私はこれでかなり新しくなりました。私自身がこれを理解することができたことに非常に驚いています。

私は "PeriodNameList"を取って解析しなければならなかったので、私はそれらの下で解析したい配列を解析するために、基本的に "PeriodNameList"を別のPHPファイルで "あらかじめ解析"次のPHP解析ページに結果が含まれ、新しい解析アルゴリズムに結果が含まれます。

<?php 

$url = "http://forecast.weather.gov/MapClick.php?lat=36.3219&lon=-96.8058&FcstType=digitalJSON"; 

// create curl resource 
$ch = curl_init(); 

// set url 
curl_setopt($ch, CURLOPT_URL, $url); 

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 

// $output contains the output string 
$output = curl_exec($ch); 

// close curl resource to free up system resources 
curl_close($ch); 

$json_a = json_decode($output,true); 



// <---------- Period List -------------> // 

    $period0 = $json_a['PeriodNameList']['0']; 
    $period1 = $json_a['PeriodNameList']['1']; 
    $period2 = $json_a['PeriodNameList']['2']; 
    $period3 = $json_a['PeriodNameList']['3']; 
    $period4 = $json_a['PeriodNameList']['4']; 
    $period5 = $json_a['PeriodNameList']['5']; 
    $period6 = $json_a['PeriodNameList']['6']; 
    $period7 = $json_a['PeriodNameList']['7']; 
    $period8 = $json_a['PeriodNameList']['8']; 
    $period9 = $json_a['PeriodNameList']['9']; 
    $period10 = $json_a['PeriodNameList']['10']; 
    $period11 = $json_a['PeriodNameList']['11']; 
    $period12 = $json_a['PeriodNameList']['12']; 
    $period13 = $json_a['PeriodNameList']['13']; 



    ?> 

それはこのようになります

...

まず解析PHPのページ(事前解析は)それから、今私は2番目の解析の次の解析フェーズの結果を含める必要があります。 ...

<?php 

require 'req/weatherinfo_hourly_period.php'; 

?> 

<?php 

$url = "http://forecast.weather.gov/MapClick.php?lat=36.3219&lon=-96.8058&FcstType=digitalJSON"; 

// create curl resource 
$ch = curl_init(); 

// set url 
curl_setopt($ch, CURLOPT_URL, $url); 

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 

// $output contains the output string 
$output = curl_exec($ch); 

// close curl resource to free up system resources 
curl_close($ch); 

$json_a = json_decode($output,true); 




// <---------- Hourly Next 12 ----------> // 

    $hourly_next_12_period = $json_a[$period0]['periodName']; 

    $hourly_next_12_time_0 = $json_a[$period0]['time'][0]; 
    $hourly_next_12_time_1 = $json_a[$period0]['time'][1]; 
    $hourly_next_12_time_2 = $json_a[$period0]['time'][2]; 
    $hourly_next_12_time_3 = $json_a[$period0]['time'][3]; 
    $hourly_next_12_time_4 = $json_a[$period0]['time'][4]; 
    $hourly_next_12_time_5 = $json_a[$period0]['time'][5]; 
    $hourly_next_12_time_6 = $json_a[$period0]['time'][6]; 
    $hourly_next_12_time_7 = $json_a[$period0]['time'][7]; 
    $hourly_next_12_time_8 = $json_a[$period0]['time'][8]; 
    $hourly_next_12_time_9 = $json_a[$period0]['time'][9]; 
    $hourly_next_12_time_10 = $json_a[$period0]['time'][10]; 
    $hourly_next_12_time_11 = $json_a[$period0]['time'][11]; 

    $hourly_next_12_windchill_0 = $json_a[$period0]['windChill'][0]; 
    $hourly_next_12_windchill_1 = $json_a[$period0]['windChill'][1]; 
    $hourly_next_12_windchill_2 = $json_a[$period0]['windChill'][2]; 
    $hourly_next_12_windchill_3 = $json_a[$period0]['windChill'][3]; 
    $hourly_next_12_windchill_4 = $json_a[$period0]['windChill'][4]; 
    $hourly_next_12_windchill_5 = $json_a[$period0]['windChill'][5]; 
    $hourly_next_12_windchill_6 = $json_a[$period0]['windChill'][6]; 
    $hourly_next_12_windchill_7 = $json_a[$period0]['windChill'][7]; 
    $hourly_next_12_windchill_8 = $json_a[$period0]['windChill'][8]; 
    $hourly_next_12_windchill_9 = $json_a[$period0]['windChill'][9]; 
    $hourly_next_12_windchill_10 = $json_a[$period0]['windChill'][10]; 
    $hourly_next_12_windchill_11 = $json_a[$period0]['windChill'][11]; 

    $hourly_next_12_windspeed_0 = $json_a[$period0]['windSpeed'][0]; 
    $hourly_next_12_windspeed_1 = $json_a[$period0]['windSpeed'][1]; 
    $hourly_next_12_windspeed_2 = $json_a[$period0]['windSpeed'][2]; 
    $hourly_next_12_windspeed_3 = $json_a[$period0]['windSpeed'][3]; 
    $hourly_next_12_windspeed_4 = $json_a[$period0]['windSpeed'][4]; 
    $hourly_next_12_windspeed_5 = $json_a[$period0]['windSpeed'][5]; 
    $hourly_next_12_windspeed_6 = $json_a[$period0]['windSpeed'][6]; 
    $hourly_next_12_windspeed_7 = $json_a[$period0]['windSpeed'][7]; 
    $hourly_next_12_windspeed_8 = $json_a[$period0]['windSpeed'][8]; 
    $hourly_next_12_windspeed_9 = $json_a[$period0]['windSpeed'][9]; 
    $hourly_next_12_windspeed_10 = $json_a[$period0]['windSpeed'][10]; 
    $hourly_next_12_windspeed_11 = $json_a[$period0]['windSpeed'][11]; 

    $hourly_next_12_winddirectioncardinal_0 = $json_a[$period0]['windDirectionCardinal'][0]; 
    $hourly_next_12_winddirectioncardinal_1 = $json_a[$period0]['windDirectionCardinal'][1]; 
    $hourly_next_12_winddirectioncardinal_2 = $json_a[$period0]['windDirectionCardinal'][2]; 
    $hourly_next_12_winddirectioncardinal_3 = $json_a[$period0]['windDirectionCardinal'][3]; 
    $hourly_next_12_winddirectioncardinal_4 = $json_a[$period0]['windDirectionCardinal'][4]; 
    $hourly_next_12_winddirectioncardinal_5 = $json_a[$period0]['windDirectionCardinal'][5]; 
    $hourly_next_12_winddirectioncardinal_6 = $json_a[$period0]['windDirectionCardinal'][6]; 
    $hourly_next_12_winddirectioncardinal_7 = $json_a[$period0]['windDirectionCardinal'][7]; 
    $hourly_next_12_winddirectioncardinal_8 = $json_a[$period0]['windDirectionCardinal'][8]; 
    $hourly_next_12_winddirectioncardinal_9 = $json_a[$period0]['windDirectionCardinal'][9]; 
    $hourly_next_12_winddirectioncardinal_10 = $json_a[$period0]['windDirectionCardinal'][10]; 
    $hourly_next_12_winddirectioncardinal_11 = $json_a[$period0]['windDirectionCardinal'][11]; 

    $hourly_next_12_windGust_0 = $json_a[$period0]['windGust'][0]; 
    $hourly_next_12_windGust_1 = $json_a[$period0]['windGust'][1]; 
    $hourly_next_12_windGust_2 = $json_a[$period0]['windGust'][2]; 
    $hourly_next_12_windGust_3 = $json_a[$period0]['windGust'][3]; 
    $hourly_next_12_windGust_4 = $json_a[$period0]['windGust'][4]; 
    $hourly_next_12_windGust_5 = $json_a[$period0]['windGust'][5]; 
    $hourly_next_12_windGust_6 = $json_a[$period0]['windGust'][6]; 
    $hourly_next_12_windGust_7 = $json_a[$period0]['windGust'][7]; 
    $hourly_next_12_windGust_8 = $json_a[$period0]['windGust'][8]; 
    $hourly_next_12_windGust_9 = $json_a[$period0]['windGust'][9]; 
    $hourly_next_12_windGust_10 = $json_a[$period0]['windGust'][10]; 
    $hourly_next_12_windGust_11 = $json_a[$period0]['windGust'][12]; 

    $hourly_next_12_iconLink_0 = $json_a[$period0]['iconLink'][0]; 
    $hourly_next_12_iconLink_1 = $json_a[$period0]['iconLink'][1]; 
    $hourly_next_12_iconLink_2 = $json_a[$period0]['iconLink'][2]; 
    $hourly_next_12_iconLink_3 = $json_a[$period0]['iconLink'][3]; 
    $hourly_next_12_iconLink_4 = $json_a[$period0]['iconLink'][4]; 
    $hourly_next_12_iconLink_5 = $json_a[$period0]['iconLink'][5]; 
    $hourly_next_12_iconLink_6 = $json_a[$period0]['iconLink'][6]; 
    $hourly_next_12_iconLink_7 = $json_a[$period0]['iconLink'][7]; 
    $hourly_next_12_iconLink_8 = $json_a[$period0]['iconLink'][8]; 
    $hourly_next_12_iconLink_9 = $json_a[$period0]['iconLink'][9]; 
    $hourly_next_12_iconLink_10 = $json_a[$period0]['iconLink'][10]; 
    $hourly_next_12_iconLink_11 = $json_a[$period0]['iconLink'][11]; 

    $hourly_next_12_pop_0 = $json_a[$period0]['pop'][0]; 
    $hourly_next_12_pop_1 = $json_a[$period0]['pop'][1]; 
    $hourly_next_12_pop_2 = $json_a[$period0]['pop'][2]; 
    $hourly_next_12_pop_3 = $json_a[$period0]['pop'][3]; 
    $hourly_next_12_pop_4 = $json_a[$period0]['pop'][4]; 
    $hourly_next_12_pop_5 = $json_a[$period0]['pop'][5]; 
    $hourly_next_12_pop_6 = $json_a[$period0]['pop'][6]; 
    $hourly_next_12_pop_7 = $json_a[$period0]['pop'][7]; 
    $hourly_next_12_pop_8 = $json_a[$period0]['pop'][8]; 
    $hourly_next_12_pop_9 = $json_a[$period0]['pop'][9]; 
    $hourly_next_12_pop_10 = $json_a[$period0]['pop'][10]; 
    $hourly_next_12_pop_11 = $json_a[$period0]['pop'][11]; 

    $hourly_next_12_relativeHumidity_0 = $json_a[$period0]['relativeHumidity'][0]; 
    $hourly_next_12_relativeHumidity_1 = $json_a[$period0]['relativeHumidity'][1]; 
    $hourly_next_12_relativeHumidity_2 = $json_a[$period0]['relativeHumidity'][2]; 
    $hourly_next_12_relativeHumidity_3 = $json_a[$period0]['relativeHumidity'][3]; 
    $hourly_next_12_relativeHumidity_4 = $json_a[$period0]['relativeHumidity'][4]; 
    $hourly_next_12_relativeHumidity_5 = $json_a[$period0]['relativeHumidity'][5]; 
    $hourly_next_12_relativeHumidity_6 = $json_a[$period0]['relativeHumidity'][6]; 
    $hourly_next_12_relativeHumidity_7 = $json_a[$period0]['relativeHumidity'][7]; 
    $hourly_next_12_relativeHumidity_8 = $json_a[$period0]['relativeHumidity'][8]; 
    $hourly_next_12_relativeHumidity_9 = $json_a[$period0]['relativeHumidity'][9]; 
    $hourly_next_12_relativeHumidity_10 = $json_a[$period0]['relativeHumidity'][10]; 
    $hourly_next_12_relativeHumidity_11 = $json_a[$period0]['relativeHumidity'][11]; 

    $hourly_next_12_temperature_0 = $json_a[$period0]['temperature'][0]; 
    $hourly_next_12_temperature_1 = $json_a[$period0]['temperature'][1]; 
    $hourly_next_12_temperature_2 = $json_a[$period0]['temperature'][2]; 
    $hourly_next_12_temperature_3 = $json_a[$period0]['temperature'][3]; 
    $hourly_next_12_temperature_4 = $json_a[$period0]['temperature'][4]; 
    $hourly_next_12_temperature_5 = $json_a[$period0]['temperature'][5]; 
    $hourly_next_12_temperature_6 = $json_a[$period0]['temperature'][6]; 
    $hourly_next_12_temperature_7 = $json_a[$period0]['temperature'][7]; 
    $hourly_next_12_temperature_8 = $json_a[$period0]['temperature'][8]; 
    $hourly_next_12_temperature_9 = $json_a[$period0]['temperature'][9]; 
    $hourly_next_12_temperature_10 = $json_a[$period0]['temperature'][10]; 
    $hourly_next_12_temperature_11 = $json_a[$period0]['temperature'][11]; 

    $hourly_next_12_weather_0 = $json_a[$period0]['weather'][0]; 
    $hourly_next_12_weather_1 = $json_a[$period0]['weather'][1]; 
    $hourly_next_12_weather_2 = $json_a[$period0]['weather'][2]; 
    $hourly_next_12_weather_3 = $json_a[$period0]['weather'][3]; 
    $hourly_next_12_weather_4 = $json_a[$period0]['weather'][4]; 
    $hourly_next_12_weather_5 = $json_a[$period0]['weather'][5]; 
    $hourly_next_12_weather_6 = $json_a[$period0]['weather'][6]; 
    $hourly_next_12_weather_7 = $json_a[$period0]['weather'][7]; 
    $hourly_next_12_weather_8 = $json_a[$period0]['weather'][8]; 
    $hourly_next_12_weather_9 = $json_a[$period0]['weather'][9]; 
    $hourly_next_12_weather_10 = $json_a[$period0]['weather'][10]; 
    $hourly_next_12_weather_11 = $json_a[$period0]['weather'][11]; 

// <---------- End Hourly Next 12 ----------> // 



?> 

そして、あなたは、私は今それ以下の配列を解析することが可能となり、既に第二の解析ページに解析された最初の解析配列を、必要に応じて、それはワットページの上部に表示さorks。ここで

は、最終結果のPHPのページ

<?php 

require 'req/weatherinfo_hourly.php'; 

?> 

<?php 
$bearing = $data_observation_wind_direction; 

$cardinalDirections = array(
'N' => array(0, 22.5), 
'N' => array(337.5, 360), 
'NE' => array(22.5, 67.5), 
'E' => array(67.5, 112.5), 
'SE' => array(112.5, 157.5), 
'S' => array(157.5, 202.5), 
'SW' => array(202.5, 247.5), 
'W' => array(247.5, 292.5), 
'NW' => array(292.5, 337.5) 
); 

foreach ($cardinalDirections as $dir => $angles) { 
if ($bearing >= $angles[0] && $bearing < $angles[1]) { 
$direction = $dir; 
break; 
    } 
} 
?> 

<!DOCTYPE html> 
<html> 
<head> 
    <title>PawneeTV Weather - Current Observation</title> 
    <link rel="stylesheet" type="text/css" href="css/current_style.css"> 
</head> 

<body> 



<?php echo $hourly_next_12_period; ?> | <?php echo $hourly_next_12_time_0; ?     > 


</body> 
</html> 

であり、これは動作します!!!!

これは、私が初心者として呼び出すことを回避する方法です。ダイナミックアレイは、配列名が常に変化しているので事前解析する必要がありますが、静的配列の名前が変わると、コードが壊れます。これはそれに追いつくための私のやり方です。

関連する問題