0

I have the following JSON records

{
  "data": {
    "content": "Michael Jordan was one of the best basketball players of all time. Scoring was Jordan stand-out skill,but he still holds a defensive NBA record, with eight steals in a half.",
    "knowledge": [
      {
        "label": "action",
        "properties": [
          {
            "type": "WikiDataId",
            "value": "Q522344"
          }
        ],
        "syncon": 628
      },
      {
        "label": "quality.human_feature",
        "properties": [
          {
            "type": "WikiDataId",
            "value": "Q205961"
          }
        ],
        "syncon": 25683
      },
      {
        "label": "person.basketball_player",
        "properties": [
          {
            "type": "WikiDataId",
            "value": "Q3665646"
          }
        ],
        "syncon": 41583
      },
      {
        "label": "object_group.property",
        "syncon": 54375
      },
      {
        "label": "other",
        "syncon": 86973
      },
      {
        "label": "event.happening",
        "syncon": 111950
      },
      {
        "label": "other",
        "syncon": 160458
      }
    ],
    "language": "en",
    "sentiment": {
      "items": [
        {
          "items": [
            {
              "lemma": "but",
              "sentiment": -1.0,
              "syncon": 160458
            },
            {
              "lemma": "stand-out",
              "sentiment": 0.0,
              "syncon": 111950
            },
            {
              "lemma": "skill",
              "sentiment": 9.5,
              "syncon": 25683
            },
            {
              "lemma": "steal",
              "sentiment": 6.0,
              "syncon": 54375
            }
          ],
          "lemma": "",
          "sentiment": 7.19,
          "syncon": -1
        },
        {
          "items": [
            {
              "lemma": "scoring",
              "sentiment": 3.5,
              "syncon": 628
            }
          ],
          "lemma": "Michael Jordan",
          "sentiment": 3.5,
          "syncon": -1
        },
        {
          "items": [
            {
              "lemma": "good",
              "sentiment": 5.0,
              "syncon": 86973
            }
          ],
          "lemma": "basketball player",
          "sentiment": 5.0,
          "syncon": 41583
        }
      ],
      "negativity": 0.0,
      "overall": 14.19,
      "positivity": 14.19
    },
    "version": "sensei: 3.3.2; disambiguator: 15.0-QNTX-2016"
  },
  "success": true
}

I was able to get the value of content successfully.

Now I want to get values for each lemma and sentiment, overall and negativity. How can I achieve that?

Below is my coding so far.

<?php

$output='
{"data":{"content":"Michael Jordan was one of the best basketball players of all time. Scoring was Jordan stand-out skill,but he still holds a defensive NBA record, with eight steals in a half.","knowledge":[{"label":"action","properties":[{"type":"WikiDataId","value":"Q522344"}],"syncon":628},{"label":"quality.human_feature","properties":[{"type":"WikiDataId","value":"Q205961"}],"syncon":25683},{"label":"person.basketball_player","properties":[{"type":"WikiDataId","value":"Q3665646"}],"syncon":41583},{"label":"object_group.property","syncon":54375},{"label":"other","syncon":86973},{"label":"event.happening","syncon":111950},{"label":"other","syncon":160458}],"language":"en","sentiment":{"items":[{"items":[{"lemma":"but","sentiment":-1.0,"syncon":160458},{"lemma":"stand-out","sentiment":0.0,"syncon":111950},{"lemma":"skill","sentiment":9.5,"syncon":25683},{"lemma":"steal","sentiment":6.0,"syncon":54375}],"lemma":"","sentiment":7.19,"syncon":-1},{"items":[{"lemma":"scoring","sentiment":3.5,"syncon":628}],"lemma":"Michael Jordan","sentiment":3.5,"syncon":-1},{"items":[{"lemma":"good","sentiment":5.0,"syncon":86973}],"lemma":"basketball player","sentiment":5.0,"syncon":41583}],"negativity":0.0,"overall":14.19,"positivity":14.19},"version":"sensei: 3.3.2; disambiguator: 15.0-QNTX-2016"},"success":true}
';

echo $output;

echo "<br><br>";


$json= json_decode($output, true);

foreach($json as $v1){


echo $content = $v1['content'];
echo "<br><br>";

/*
foreach($json as $v1['items']){
echo $lemma = $v1['items']['lemma'];
echo $sentiment = $v1['items']['sentiment'];

}


echo $negativity = $v1['negativity'];
echo $overall =    $v1['overall'];
*/

}


?>

A Screenshot pointing those Propertises I want to get its values

halfer
  • 19,824
  • 17
  • 99
  • 186
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
  • What **exactly** is not working with the given code? – Nico Haase Feb 16 '21 at 16:00
  • the values for **lemma** and **sentiment**, **overall** and **negativity** is what am trying to get. it throws error in the code Thats is why I comment it out in the code. I also pointed it out in the screenshot I attached – Nancy Moore Feb 16 '21 at 16:03
  • 1
    Please share more details - "it throws error" is pretty broad, what does that mean? – Nico Haase Feb 16 '21 at 16:10
  • `foreach($json as $v1['items'])` - what should that code do? `$v1` is already defined, and I don't think that it is a good idea to iterate over `$json` in a nested loop. Why not iterate over whatever `$v1` contains? – Nico Haase Feb 16 '21 at 16:12
  • I think you have an _"undefined index"_ notice (see https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined). – Syscall Feb 16 '21 at 16:14
  • The information about a thrown error should be in the question itself. I can cast a reopen vote on this if the post is satisfactorily edited. – halfer Feb 23 '21 at 09:08

1 Answers1

2

You can use array_walk_recursive function to achieve this.

$output='
{"data":{"content":"Michael Jordan was one of the best basketball players of all time. Scoring was Jordan stand-out skill,but he still holds a defensive NBA record, with eight steals in a half.","knowledge":[{"label":"action","properties":[{"type":"WikiDataId","value":"Q522344"}],"syncon":628},{"label":"quality.human_feature","properties":[{"type":"WikiDataId","value":"Q205961"}],"syncon":25683},{"label":"person.basketball_player","properties":[{"type":"WikiDataId","value":"Q3665646"}],"syncon":41583},{"label":"object_group.property","syncon":54375},{"label":"other","syncon":86973},{"label":"event.happening","syncon":111950},{"label":"other","syncon":160458}],"language":"en","sentiment":{"items":[{"items":[{"lemma":"but","sentiment":-1.0,"syncon":160458},{"lemma":"stand-out","sentiment":0.0,"syncon":111950},{"lemma":"skill","sentiment":9.5,"syncon":25683},{"lemma":"steal","sentiment":6.0,"syncon":54375}],"lemma":"","sentiment":7.19,"syncon":-1},{"items":[{"lemma":"scoring","sentiment":3.5,"syncon":628}],"lemma":"Michael Jordan","sentiment":3.5,"syncon":-1},{"items":[{"lemma":"good","sentiment":5.0,"syncon":86973}],"lemma":"basketball player","sentiment":5.0,"syncon":41583}],"negativity":0.0,"overall":14.19,"positivity":14.19},"version":"sensei: 3.3.2; disambiguator: 15.0-QNTX-2016"},"success":true}
';


$json = json_decode($output, true);

$resArr = [];
$keys = ['lemma', 'sentiment', 'overall', 'negativity'];

array_walk_recursive($json, function($value, $key) use(&$keys, &$resArr) {
    
    if(in_array($key, $keys)){
        $resArr[] = [$key => $value];
    }
    
});

print_r($resArr);

Output:

 Array
(
    [0] => Array
        (
            [lemma] => but
        )

    [1] => Array
        (
            [sentiment] => -1
        )

    [2] => Array
        (
            [lemma] => stand-out
        )

    [3] => Array
        (
            [sentiment] => 0
        )

    [4] => Array
        (
            [lemma] => skill
        )

    [5] => Array
        (
            [sentiment] => 9.5
        )

    [6] => Array
        (
            [lemma] => steal
        )

    [7] => Array
        (
            [sentiment] => 6
        )

    [8] => Array
        (
            [lemma] => 
        )

    [9] => Array
        (
            [sentiment] => 7.19
        )

    [10] => Array
        (
            [lemma] => scoring
        )

    [11] => Array
        (
            [sentiment] => 3.5
        )

    [12] => Array
        (
            [lemma] => Michael Jordan
        )

    [13] => Array
        (
            [sentiment] => 3.5
        )

    [14] => Array
        (
            [lemma] => good
        )

    [15] => Array
        (
            [sentiment] => 5
        )

    [16] => Array
        (
            [lemma] => basketball player
        )

    [17] => Array
        (
            [sentiment] => 5
        )

    [18] => Array
        (
            [negativity] => 0
        )

    [19] => Array
        (
            [overall] => 14.19
        )

)

Updated: To store values for all keys in separate arrays

array_walk_recursive($json, function($value, $key) use(&$keys, &$resArr) {
    
    if(in_array($key, $keys)){
        $resArr[$key][] = $value;
    }
    
});

print_r($resArr['lemma']);
print_r($resArr['sentiment']);
print_r($resArr['negativity']);
print_r($resArr['overall']);

Output:

Array // lemma
(
    [0] => but
    [1] => stand-out
    [2] => skill
    [3] => steal
    [4] => 
    [5] => scoring
    [6] => Michael Jordan
    [7] => good
    [8] => basketball player
)
Array // sentiment
(
    [0] => -1
    [1] => 0
    [2] => 9.5
    [3] => 6
    [4] => 7.19
    [5] => 3.5
    [6] => 3.5
    [7] => 5
    [8] => 5
)
Array // negativity
(
    [0] => 0
)
Array // overall
(
    [0] => 14.19
)

echo all values

// print all lemma 
echo $lemma = implode(", ", $resArr['lemma']); 
echo "<br><br>";  
// print all sentiment 
echo $sentiment = implode(", ", $resArr['sentiment']);
echo "<br><br>";  
// print overall 
echo $overall = implode(", ", $resArr['overall']); 
echo "<br><br>";   
//print negativity 
echo $negativity = implode(", ", $resArr['negativity']);
echo "<br><br>";
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19