3
 Array
(
    [requestId] => 69a6#16a49c12847
    [result] => Array
        (
            [0] => Array
                (
                    [seq] => 0
                    [marketoGUID] => d6a515c3-b1d4-4b68-bc61-6d40bf0387a5
                    [status] => created
                )

        )

    [success] => 1
)

I need to get status value from this array. I am getting like this $responseDecode['result'][0]['status']. But it's not working

  • what is the error you facing? – Rizwan Khan Apr 23 at 11:26
  • Does it is json responce? And which version of magento? – Dhiren Vasoya Apr 23 at 11:34
  • Yes, It is json response $responseDecode = json_decode(($response), true); and magento2 version is 2.2.6 – Shiwani Apr 23 at 11:37
  • With this code I am getting error in console VM10057:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 – Shiwani Apr 23 at 12:31
1

You can use this below code :

/**
 * @var \Magento\Framework\Json\Helper\Data
 */
protected $jsonHelper;

/**
 * [__construct description]
 * @param \Magento\Framework\Json\Helper\Data              $jsonHelper        [description]
 */
public function __construct(
    .......
    \Magento\Framework\Json\Helper\Data $jsonHelper,
    .......
) {
    .......
    $this->jsonHelper = $jsonHelper;
    .......
}

public function yourFunction() {
    $strToArr = $this->jsonHelper->jsonDecode($response);
    echo $strToArr['result']['0']['status'];
}
0

There's no need to wrap the $response with parenthesis

$responseDecode = json_decode($response, true);  

And you can get the status the same way you did:

$responseDecode['result']['0']['status']  

Note: 0 should be inside single quote

  • I tried also like this $responseDecode = json_decode($response, true); – Shiwani Apr 23 at 12:01
  • yeah but you should take note that 0 should be inside single quote also otherwise it will return an undefined offset – fmsthird Apr 23 at 12:02
  • @Shiwani did you try it? did it work or you get any errors? – fmsthird Apr 23 at 12:11
  • $responseDecode['result']['0']['status'] Code is not working – Shiwani Apr 23 at 12:12
  • try echo $responseDecode['result']['0']['status']; and check what you get. It should work as per info given above – fmsthird Apr 23 at 12:17
0

You just need to decode your json data and then you can print value.

$array = json_decode($responseDecode); echo $array['result'][0]['status'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.