duanhai4046 2016-05-11 19:30 采纳率: 100%
浏览 40

将json数据从数组转换为对象并打印为表[duplicate]

This question already has an answer here:

Guys any help is appreciated. New to php & learning as I go. My Objective: change the array to an object and print it out as a table. Below is my json data which is save as people.txt file.

{
    "people": [
      {

        "first_name": "John", 
        "last_name": "Smith",
        "city": "NY",
        "state": "New York"   
      },
      {
        "first_name": "Jane",
        "last_name": "Smith",
        "city": "Paris",
        "state": "France"
      },
      {  
        "first_name": "John", 
        "last_name": "Smith",
        "city": "Orlando",  
        "state": "Florida"
      },
      {  
        "first_name": "Jane",
        "last_name": "Smith",
        "city": "Toronto",
        "state": "Canada", 
      }
  ]

}

//this issue is when I try to change the from array to an object using $people_data = json_decode ($json_data); without the second parameter "true". or even using $people_data = json_decode (json_encode ($json_data)); //HERE IS MY PHP CODE

$json_data = file_get_contents ("people.txt");

// I was able to use $people_data = json_decode ($json_data, true); to output my table just fine by changing my data to an array.

$people_data = json_decode ($json_data, true);

$output = "<table border='1' style=' border-collapse:collapse; 'width=25%'>";

    $output .= "<tr>";
        $output .= "<th>". "First Name" ."</th>";
        $output .= "<th>". "Last Name" ."</th>";
        $output .= "<th>". "City" ."</th>";
        $output .= "<th>". "State" ."</th>";
    $output .= "</tr>";

foreach ($people_data ["people"] as $person) {

    $output .= "<tr>";
        $output .= "<td>". $person ["first_name"] ."</td>";
        $output .= "<td>". $person ["last_name"] ."</td>";
        $output .= "<td>". $person ["city"] ."</td>";
        $output .= "<td>". $person ["state"] ."</td>";
    $output .= "</tr>";  
}
    $output .= "</table>"; 

  echo $output;

?>

this is a pic of my results using an "array", trying to get the same result as "object". json table

</div>
  • 写回答

1条回答 默认 最新

  • douzhi7661 2016-05-12 00:17
    关注

    in your loop, just set it as an array.

    It is especially useful to dynamically set, unset or modify object properties,it s made for, especially in classes to avoid volatile destruction. Easier to deal with too than objects, also faster as , converted as an array, it uses indexes (kinda like in mysql):

    $list = ["first_name", "last_name", "city", "state"]; // set outside loop
    $type = "people";
    foreach ($people_data [$type] as $key => $person) {
        $person = (array) $person; // convert to array
        unset($people_data[$type][$key]); // purge unnecessary ram
        $output .= '<tr id="' . $type . 'Id_' . $key . '">'; // use single quotes for perf gain
        foreach ($list as $key2 => $val) {
            $output .= '<td class="' . $val . '">' . $person [$val] . '</td>';
        }
        $output .= '</tr>';
    }
    

    Bonus:

    You are on the right track to use string concat (.=). and only echo once. This is 10 times faster. multiple echo in a script is a perf killer.

    In the same manner, use simple quotes for your concat and not double quotes so that PHP does not have to check for variables inside it.

    All in all, for small stuff or low traffic this does not change anything but when it comes to heavier loads, you double the perfs of your scripts.

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图