dongmi1941 2012-02-27 03:48
浏览 17
已采纳

Php stdClass对象到html列表

Anyone know how to do this?

I have the code below stored in a variable and would like to echo it in different ways, such as:

  • sum of values by country
  • sum of values by city
  • sum of values by state

Something like:

country-a: 220
country-b: 80
country-c: 70

state-a: 220
state-b: 80
state-c: 70

city-a: 220
city-b: 80
city-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

)

Appreciate your help!

  • 写回答

1条回答 默认 最新

  • doukunsan5553 2012-02-27 06:37
    关注

    Please let me know if you don't understand the code or need help with other functions. I also shorted the json code. You could probably implement more functions to the class and create a nive FB-stats-interface for yourself.

    //EDIT: ok, I created a small API-interface for you. It contains one include file, an example of the usage and 2 example-json-files. Please name the files as followed and put them all in the same folder.

    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":""}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条