0

I have a JSON data object like so (stored in $json):

[{
    "Komoditas": "Beras",
    "09\/08\/2017": "10.612",
    "10\/08\/2017": "10.623",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Gula",
    "09\/08\/2017": "13.242",
    "10\/08\/2017": "13.235",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Minyak Goreng",
    "09\/08\/2017": "11.399",
    "10\/08\/2017": "11.395",
    "Sat": "Lt",
    "Ket": ""
}, {
    "Komoditas": "Tepung Terigu",
    "09\/08\/2017": "9.031",
    "10\/08\/2017": "9.026",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Kedelai",
    "09\/08\/2017": "10.775",
    "10\/08\/2017": "10.775",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Daging Sapi",
    "09\/08\/2017": "117.323",
    "10\/08\/2017": "117.372",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Daging Ayam",
    "09\/08\/2017": "33.024",
    "10\/08\/2017": "32.953",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Telur Ayam",
    "09\/08\/2017": "22.961",
    "10\/08\/2017": "22.929",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Cabe",
    "09\/08\/2017": "30.054",
    "10\/08\/2017": "29.791",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Bawang",
    "09\/08\/2017": "31.222",
    "10\/08\/2017": "31.251",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Susu",
    "09\/08\/2017": "10.446",
    "10\/08\/2017": "10.436",
    "Sat": "Gr",
    "Ket": ""
}, {
    "Komoditas": "Jagung",
    "09\/08\/2017": "7.154",
    "10\/08\/2017": "7.140",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Ikan",
    "09\/08\/2017": "76.826",
    "10\/08\/2017": "77.058",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Garam",
    "09\/08\/2017": "9.869",
    "10\/08\/2017": "10.079",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Mie Instan",
    "09\/08\/2017": "2.421",
    "10\/08\/2017": "2.428",
    "Sat": "Bks",
    "Ket": ""
}, {
    "Komoditas": "Kacang",
    "09\/08\/2017": "25.594",
    "10\/08\/2017": "25.510",
    "Sat": "Kg",
    "Ket": ""
}, {
    "Komoditas": "Ketela Pohon",
    "09\/08\/2017": "5.410",
    "10\/08\/2017": "5.380",
    "Sat": "Kg",
    "Ket": ""
}]

I want to filter it so that I only get the data from "Komoditas "**Beras", the result for the given JSON should look like this:

[{
    "Komoditas": "Beras",
    "09\/08\/2017": "10.612",
    "10\/08\/2017": "10.623",
    "Sat": "Kg",
    "Ket": ""
}]

I've tried the following code but it shows the error "Trying to get property of non-object":

 <?php
    $result = (json_decode($json));
    echo $result->Komoditas['Beras']; 
   ?>

Can anyone tell me what I'm doing wrong please? Thanks!

Rick van Lieshout
  • 2,276
  • 2
  • 22
  • 39
Maestro Vladimir
  • 1,186
  • 4
  • 18
  • 38
  • your desire result is also a `json` while `json_decode` convert json into array. – urfusion Aug 16 '17 at 12:53
  • Do you just want the first element? or do you want to search for `Beras`? – mickmackusa Aug 16 '17 at 12:56
  • @mickmackusa for my website search data per rows example : Beras => call json search Beras Gula => call json search Gula – Maestro Vladimir Aug 16 '17 at 12:57
  • How can this be duplicate ? Please read the author's need. The link and the question are so different. Congrats. – Michael GEDION Aug 16 '17 at 13:32
  • The selected answer on the duplicate link uses `array_search` and `array_column` to target the desired substring. https://stackoverflow.com/a/6661561/2943403 This is the technique to use in this case. – mickmackusa Aug 16 '17 at 13:40
  • Ok i got you. The question was how to deal and extract from json with `09\/08\/2017` format. May be YOU know how to use JSON_UNESCAPED_SLASHES just by seeing the answers. Well others may not. And in the link there is no JSON_UNESCAPED_SLASHES, json etc... Duplicate ? – Michael GEDION Aug 16 '17 at 13:40
  • @MichaelGEDION That issue is, yes, another duplicate: https://stackoverflow.com/a/10210433/2943403 This question is under-researched. – mickmackusa Aug 16 '17 at 13:44

1 Answers1

1

According to the desire output you have mention

[{ "Komoditas" : "Beras", "09/08/2017" : "10.612", "10/08/2017" : "10.623", "Sat" : "Kg", "Ket" : "" }]

below code will work for you

$json = '[{"Komoditas":"Beras","09\/08\/2017":"10.612","10\/08\/2017":"10.623","Sat":"Kg","Ket":""},{"Komoditas":"Gula","09\/08\/2017":"13.242","10\/08\/2017":"13.235","Sat":"Kg","Ket":""},{"Komoditas":"Minyak Goreng","09\/08\/2017":"11.399","10\/08\/2017":"11.395","Sat":"Lt","Ket":""},{"Komoditas":"Tepung Terigu","09\/08\/2017":"9.031","10\/08\/2017":"9.026","Sat":"Kg","Ket":""},{"Komoditas":"Kedelai","09\/08\/2017":"10.775","10\/08\/2017":"10.775","Sat":"Kg","Ket":""},{"Komoditas":"Daging Sapi","09\/08\/2017":"117.323","10\/08\/2017":"117.372","Sat":"Kg","Ket":""},{"Komoditas":"Daging Ayam","09\/08\/2017":"33.024","10\/08\/2017":"32.953","Sat":"Kg","Ket":""},{"Komoditas":"Telur Ayam","09\/08\/2017":"22.961","10\/08\/2017":"22.929","Sat":"Kg","Ket":""},{"Komoditas":"Cabe","09\/08\/2017":"30.054","10\/08\/2017":"29.791","Sat":"Kg","Ket":""},{"Komoditas":"Bawang","09\/08\/2017":"31.222","10\/08\/2017":"31.251","Sat":"Kg","Ket":""},{"Komoditas":"Susu","09\/08\/2017":"10.446","10\/08\/2017":"10.436","Sat":"Gr","Ket":""},{"Komoditas":"Jagung","09\/08\/2017":"7.154","10\/08\/2017":"7.140","Sat":"Kg","Ket":""},{"Komoditas":"Ikan","09\/08\/2017":"76.826","10\/08\/2017":"77.058","Sat":"Kg","Ket":""},{"Komoditas":"Garam","09\/08\/2017":"9.869","10\/08\/2017":"10.079","Sat":"Kg","Ket":""},{"Komoditas":"Mie Instan","09\/08\/2017":"2.421","10\/08\/2017":"2.428","Sat":"Bks","Ket":""},{"Komoditas":"Kacang","09\/08\/2017":"25.594","10\/08\/2017":"25.510","Sat":"Kg","Ket":""},{"Komoditas":"Ketela Pohon","09\/08\/2017":"5.410","10\/08\/2017":"5.380","Sat":"Kg","Ket":""}]';
$result = json_encode(json_decode($json),JSON_UNESCAPED_SLASHES);
echo "<pre>"; print_r($result);

Output

[{"Komoditas":"Beras","09/08/2017":"10.612","10/08/2017":"10.623","Sat":"Kg","Ket":""},{"Komoditas":"Gula","09/08/2017":"13.242","10/08/2017":"13.235","Sat":"Kg","Ket":""},{"Komoditas":"Minyak Goreng","09/08/2017":"11.399","10/08/2017":"11.395","Sat":"Lt","Ket":""},{"Komoditas":"Tepung Terigu","09/08/2017":"9.031","10/08/2017":"9.026","Sat":"Kg","Ket":""},{"Komoditas":"Kedelai","09/08/2017":"10.775","10/08/2017":"10.775","Sat":"Kg","Ket":""},{"Komoditas":"Daging Sapi","09/08/2017":"117.323","10/08/2017":"117.372","Sat":"Kg","Ket":""},{"Komoditas":"Daging Ayam","09/08/2017":"33.024","10/08/2017":"32.953","Sat":"Kg","Ket":""},{"Komoditas":"Telur Ayam","09/08/2017":"22.961","10/08/2017":"22.929","Sat":"Kg","Ket":""},{"Komoditas":"Cabe","09/08/2017":"30.054","10/08/2017":"29.791","Sat":"Kg","Ket":""},{"Komoditas":"Bawang","09/08/2017":"31.222","10/08/2017":"31.251","Sat":"Kg","Ket":""},{"Komoditas":"Susu","09/08/2017":"10.446","10/08/2017":"10.436","Sat":"Gr","Ket":""},{"Komoditas":"Jagung","09/08/2017":"7.154","10/08/2017":"7.140","Sat":"Kg","Ket":""},{"Komoditas":"Ikan","09/08/2017":"76.826","10/08/2017":"77.058","Sat":"Kg","Ket":""},{"Komoditas":"Garam","09/08/2017":"9.869","10/08/2017":"10.079","Sat":"Kg","Ket":""},{"Komoditas":"Mie Instan","09/08/2017":"2.421","10/08/2017":"2.428","Sat":"Bks","Ket":""},{"Komoditas":"Kacang","09/08/2017":"25.594","10/08/2017":"25.510","Sat":"Kg","Ket":""},{"Komoditas":"Ketela Pohon","09/08/2017":"5.410","10/08/2017":"5.380","Sat":"Kg","Ket":""}]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
urfusion
  • 5,528
  • 5
  • 50
  • 87