Search code examples
phpjsonyii2jsonresultjsonresponse

Yii2- How to get a single value from a JSON decoded array


I have a JSON decoded array like below

array(1) {
[0]=>
object(stdClass)#145 (31) {
["ref_no"]=>
string(15) "28374140183000U"
["meter_msn"]=>
string(12) "002999000172"
["billing_msn"]=>
string(5) "04513"
["latitude"]=>
string(10) "25.5063814"
["longitude"]=>
string(10) "69.0137568"
["tarrif"]=>
string(2) "45"
["s_load"]=>
string(5) "22.38"
["so_person_name"]=>
string(0) ""
["so_phone_number"]=>
string(0) ""
["so_person_designation"]=>
string(0) ""
["customer_id"]=>
string(11) "37010720390"
["istallation_status"]=>
string(9) "Installed"
["site_issues"]=>
string(19) "No hand-over person"
["install_id"]=>
string(26) "28372250046142R_1516705592"
["created_date"]=>
string(19) "2018-23-01 12:00:21"
["consumer_name"]=>
string(52) "T.M.A JHUNDO MARI AT VILL LAKHA DINO CORI S.TOWN MPK"
["consumer_address"]=>
string(52) "T.M.A JHUNDO MARI AT VILL LAKHA DINO CORI S.TOWN MPK"
["comm_status"]=>
string(8) "Verified"
["auth_key"]=>
string(32) "key"
["ct_ratio"]=>
string(3) "200"
["ct_ratio_quantity"]=>
string(1) "4"
["cable_type"]=>
string(5) "19/83"
["cable_length"]=>
string(3) "4.2"
["atb_installed"]=>
string(3) "Yes"
["meter_type"]=>
string(7) "L.T.TOU"
["old_meter_power"]=>
string(1) "5"
["old_meter_reactive_power"]=>
string(1) "3"
["new_meter_power"]=>
string(1) "2"
["new_meter_reactive_power"]=>
string(1) "3"
["site_images_name"]=>
array(0) {
}
["doc_images_name"]=>
array(0) {
}}}

From this output I want to get meter_msn. The JSON response is in a file and I am getting the contents of the file like below

 $fp = fopen('debugeeeeeee.txt', 'w+');
    fwrite($fp, file_get_contents('php://input'));
    fclose($fp);
    $inputs = json_decode(file_get_contents('php://input'));

Now for taking out meter_msn I have tried $inputs->meter_msn which gives me unidentified error and inputs['meter_msn'] which gives me Undefined index: meter_msn error.

I have searched many articles and questions on it and tried every solution but still unable to get the result. How can I get it ?

Any help would be highly appreciated.


Solution

  • Your response is an object as the first member of an array. This will work:

    $inputs[0]->meter_msn
    

    Read that var_dump again:

    array(1) {
    [0]=>
    object(stdClass)#145 (31) {