-2
$status_key ="status"

$response = {"error":false,"data":{"id":16420728,"order_id":"5000","mobile_no":"9995088810","amount":20,"balance":46.89,"status":"failure","tnx_id":"","response":"Transaction Failed"}}"


$result = json_decode($response);

this prints:

{ 
    ["error"]=> bool(false) 
    ["data"]=> object(stdClass)#24 (8) { 
        ["id"]=> int(16420728) 
        ["order_id"]=> string(4) "5000" 
        ["mobile_no"]=> string(10) "9995088810" 
        ["amount"]=> int(20) 
        ["balance"]=> float(46.89) 
        ["status"]=> string(7) "failure" 
        ["tnx_id"]=> string(0) "" 
        ["response"]=> string(18) "Transaction Failed" 
    }
}

now i am checking for specified key

var_dump(array_key_exist(status_key,result));

when i do var_dump() this function, it returns "bool(false)"

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

1 Answers1

1

You have lot of syntax mistake as well as you need to modify code a bit like below:

var_dump(array_key_exists($status_key,$result->data));
//- --------------------^-^-----------^-------^--^-- missing those

Output:- https://3v4l.org/uWB7s

For a better search approach, take help of this thread: Check if specific array key exists in multidimensional array - PHP

$result = json_decode($response, true);

var_dump(findKey($result, $status_key));

function findKey($array, $keySearch)
{
    foreach ($array as $key => $item) {
        if ($key == $keySearch) {
            return true;
        } elseif (is_array($item) && findKey($item, $keySearch)) {
            return true;
        }
    }
    return false;
}

Output:-https://3v4l.org/KaX9F

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98