2012-02-27 14 views
1

誰でもこれを行う方法は分かりますか?PHP stdClassオブジェクトをHTMLにリスト

私は変数に保存されている以下のコードを持っているなど、さまざまな方法でそれをエコーし​​たいと思います:国別の値の

  • 合計市
  • 和のことで値の
  • 合計状態によって値

のような何か:

国-:220
国-B:80
国-C:70

状態:220
状態-B:80
状態-C:70

都市:220
都市-B:80
都市-C:70

stdClass Object 
(
    [city-a, state-a, country-a] => 100 
    [city-a, state-a, country-a] => 120 
    [city-b, state-b, country-b] => 80 
    [city-c, state-c, country-c] => 70 

) 

Aあなたの助けを惜しまない!

+3

refor入ってくるデータをマッティングすると、これはずっと楽になります。データソースは何ですか? –

+2

構造が非常に不適切なので、私は汚れて答えを出す気がします。 – h0tw1r3

+0

@ダゴン:データソースはFacebookのグラフAPI - Insights - page_fans_cityからのものです。 FB APIエクスプローラhttps://developers.facebook.com/tools/explorerをご覧ください。 –

答えて

1

コードを理解していないか、他の機能に関するヘルプが必要な場合は、教えてください。 jsonコードも短くしました。クラスにもっと多くの機能を実装し、あなた自身のために必要なFB-stats-interfaceを作成することができます。

//編集: ok、私はあなたのための小さなAPIインターフェイスを作成しました。 1つのインクルードファイル、使用例と2つのexample-json-filesが含まれています。ファイルの名前をつけて、同じフォルダに入れてください。

のindex.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<title>Your title</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 

<body> 

<?php 
//include files 
include 'Stats.php'; 

//create an object of Stats 
$stats = new Stats(); 

//parse any number of json files 
$stats->parseJson("json1.txt"); 
$stats->parseJson("json2.txt"); 

//call the functions 

print "<h1>var_dump of the data</h1>"; 

print '<pre>' . print_r($stats->getList(), true) . '</pre>'; 
print "<h1>By Country</h1>"; 
print "Austria:".$stats->getSumByCountry("Austria")."<br />"; 
print "Germany:".$stats->getSumByCountry("Germany")."<br />"; 
print "USA:".$stats->getSumByCountry("USA")."<br />"; 

print "<h1>By State</h1>"; 
print "Tirol:".$stats->getSumByState("Tirol")."<br />"; 
print "Bayern:".$stats->getSumByState("Bayern")."<br />"; 
print "Massachusetts:".$stats->getSumByState("Massachusetts")."<br />"; 

print "<h1>By City</h1>"; 
print "Austria:".$stats->getSumByCity("Imst")."<br />"; 
print "Berlin:".$stats->getSumByCity("Berlin")."<br />"; 
print "Los Angeles:".$stats->getSumByCity("Los Angeles")."<br />"; 




?> 
</body> 
</html> 

Stats.php:

<?php 
class Stats{ 
    private $cities; 

    public function addCity($city){ 
     $this->cities[]=$city; 
    } 


    public function parseJson($jsonFile){ 
     $jsonData = file_get_contents($jsonFile); 
     if($jsonData == null || $jsonData==""){ 
      die("Error: failed to load $jsonFile"); 
     } 

     $object = json_decode($jsonData); 
     //naviagte through the object to the data we want to have 
     $data = $object->data; 
     $temp_item = $data[0]; 
     $temp_value = $temp_item->values; 
     $temp_item2 = $temp_value[0]; 
     $value = $temp_item2->value; 

     //now create an entity of City for each entry and put it in the list of this class 
     foreach ($value as $key=> $item){ 
      $placeArray = explode(",",$key); 
      $city = trim($placeArray[0]); 
      $state = trim($placeArray[1]); 
      $country = trim($placeArray[2]); 
      $sum = $item; 
      $this->addCity(new City($sum, $city, $state, $country)); 
     } 
    } 

    public function getSumByCity($name){ 
     $sum = 0; 
     foreach ($this->cities as $city){ 
      if(strcmp($city->name,$name) == 0){ 
       $sum += $city->sum; 
      } 
     } 
     return $sum; 
    } 

    public function getSumByState($state){ 
     $sum = 0; 
     foreach ($this->cities as $city){ 
      echo ""; 
      if(strcmp($city->state,$state) == 0){ 
       $sum += $city->sum; 
      } 
     } 
     return $sum; 
    } 
    public function getSumByCountry($country){ 
     $sum = 0; 
     foreach ($this->cities as $city){ 
      if(strcmp($city->country,$country) == 0){ 
       $sum += $city->sum; 
      } 
     } 
     return $sum; 
    } 

    public function getList(){ 
     return $this->cities; 
    } 
} 

class City{ 
    public $sum, $name, $state, $country; 
    function __Construct($sum, $name, $state, $country){ 
     $this->name = $name; 
     $this->sum = $sum; 
     $this->state = $state; 
     $this->country = $country; 
    } 
} 
?> 

json1.txt

{"data":[ 
    {"id":"SOMEID/insights/page_fans_city/lifetime", 
    "name":"page_fans_city", 
    "period":"lifetime", 
    "values":[ 
     {"value": 
      {"Wattens, Tirol, Austria":160, 
      "Imst, Tirol, Austria":123, 
      "Zirl, Tirol, Austria":117}, 
     "end_time":""}], 
    "title":"", 
    "description":""}], 
"paging": 
    {"previous":"", 
    "next":""} 
} 

json2.txt:

{"data":[ 
    {"id":"SOMEID/insights/page_fans_city/lifetime", 
    "name":"page_fans_city", 
    "period":"lifetime", 
    "values":[ 
     {"value": 
      {"Boston, Massachusetts, USA":199, 
      "Los Angeles, California, USA":55}, 
     "end_time":""}], 
    "title":"", 
    "description":""}], 
"paging": 
    {"previous":"", 
    "next":""} 
} 
+0

おかげミル@Johannes残念ながら、私はそれを動作させることはできません。私がしているのは、file_get_contents(JSON DATAへのパス)です。それをjson_decode()に入れてください。また、別のJSON入力(異なるカウンターや都市など)に対してそのコードを実行できるように、自動的にグループ化することも可能です。あなたの助けをもう一度感謝し、本当にそれを感謝します! PS。私が得るエラーは次のとおりです。foreach() –

+0

の引数が無効です。配列が正しく埋められていないためです。 file_get_contents(http:// json全体へのパス)を行う場合はもちろん、トップノードから国/都市データをナビゲートする必要があります; 私のコードは正確なコピー&ペーストではありません。そして、複数のjson入力のコードを再利用することができます! $ json-> file_get_contents(JSONデータへのパス)$ nodeX = $ json-> NodeX .... $ jsonDate - > $ Node_OF_COUNTRIESを試してください。 オフィスまで家に帰るまで10時間待つ; –

+0

こんにちは@Hohannes、それに感謝しますが、今私は全く混乱しています:(申し訳ありませんが、おそらく言いたいことですが、私はPHPの初心者です。 –

関連する問題