0

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

uno10
  • 11
  • 3

1 Answers1

0

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.

cpugourou
  • 775
  • 7
  • 11