0

I am working on a website in which I want to extract JSON variable in php. The code which I have used for that is:

<div class="tab-pane" id="policies">
   <p> 
      <span class="heading_size">policies:</span>

   </p>
   <p class="text-justify mb-0 pb-5">
      <?php 
         echo strtolower($data['item']->company_policy->description); 
         ?>
   </p>
</div>

In the above code the following line echo strtolower($data['item']->company_policy->description); doesn't seems to print anything but for the debugging purpose when I have added <?php echo $data['item']; ?>, it is extracting the following snippets of JSON:

"company_policy": {
    "company_policy_id": 2,
    "uuid": "B4d2aa790",
    "name": "Reasonable",
    "description": "a.) Hello World. ",
    "cutoff_time": 96,
    "percentage": "1.00"
},

In order to extract the description from the above JSON, it is understood that I should be using echo strtolower($data['item']->company_policy->description); but it doesn't seems to extract anything as explained above.


Problem Statement:

I am wondering what changes I should make in the above echo statement so that I am successfully able to fetch the data from the JSON.

flash
  • 1,564
  • 3
  • 27
  • 87
  • `json_decode($data['item'], true)` – Khoa TruongDinh Aug 16 '18 at 02:10
  • @KhoaTruongDinh What should I be writing here ? `echo strtolower($data['item']->company_policy->description);` – flash Aug 16 '18 at 02:11
  • 1
    @flash A JSON formatted string, as far as PHP is concerned, is just a string. As said before, you need to use `json_decode()`. When debugging your code, always make sure you [turn on error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) or check the error log file. You likely would have received some sort of error when doing this because you are attempting to treat a string like an object. – Mike Aug 16 '18 at 02:15
  • @Mike Thanks for the answer. Can you let me know what exactly I have to use ? – flash Aug 16 '18 at 02:35
  • @Mike like in place of this echo `strtolower($data['item']->company_policy->description);` what I should be using ? – flash Aug 16 '18 at 02:37
  • Did you read the link in Paul Crovella's comment above? – Mike Aug 16 '18 at 02:37
  • yes I read his comment. – flash Aug 16 '18 at 02:43
  • @flash Did you click on the link in his comment, go to the other page, and read the first answer there? That's pretty in depth. – Mike Aug 16 '18 at 02:44

2 Answers2

2

First you have decode json data by using json_decode

<?php $jsonData =  json_decode($data['item'], true); ?>

<div class="tab-pane" id="policies">
   <p> 
      <span class="heading_size">policies:</span>

   </p>
   <p class="text-justify mb-0 pb-5">
      <?php 
         echo strtolower($jsonData['company_policy']['description']); 
         ?>
   </p>
</div>
Krishna Jonnalagadda
  • 1,894
  • 1
  • 10
  • 25
  • Ok, it worked. Thanks for the help. I am wondering, why its not working in my way ? I am very curious regarding that. – flash Aug 16 '18 at 03:26
0

As per the documentation, you need to specify if you want an associative array instead of an object from json_decode, this would be the code: json_decode($jsondata, true);

Eduardo Herrera
  • 79
  • 1
  • 15