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

ml13007
5755 silver badges17 bronze badges
asked Apr 23, 2019 at 11:23
4
  • what is the error you facing? Commented Apr 23, 2019 at 11:26
  • Does it is json responce? And which version of magento? Commented Apr 23, 2019 at 11:34
  • Yes, It is json response $responseDecode = json_decode(($response), true); and magento2 version is 2.2.6 Commented Apr 23, 2019 at 11:37
  • With this code I am getting error in console VM10057:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 Commented Apr 23, 2019 at 12:31

3 Answers 3

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'];
}
answered Apr 23, 2019 at 12:03
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

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

answered Apr 24, 2019 at 2:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.