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– Rizwan Khan2019年04月23日 11:26:47 +00:00Commented Apr 23, 2019 at 11:26
-
Does it is json responce? And which version of magento?Dhiren Vasoya– Dhiren Vasoya2019年04月23日 11:34:06 +00:00Commented Apr 23, 2019 at 11:34
-
Yes, It is json response $responseDecode = json_decode(($response), true); and magento2 version is 2.2.6Shomita– Shomita2019年04月23日 11:37:45 +00:00Commented 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 0Shomita– Shomita2019年04月23日 12:31:28 +00:00Commented Apr 23, 2019 at 12:31
3 Answers 3
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'];
}
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);Shomita– Shomita2019年04月23日 12:01:36 +00:00Commented Apr 23, 2019 at 12:01
-
yeah but you should take note that
0should be inside single quote also otherwise it will return an undefined offsetfmsthird– fmsthird2019年04月23日 12:02:54 +00:00Commented Apr 23, 2019 at 12:02 -
@Shiwani did you try it? did it work or you get any errors?fmsthird– fmsthird2019年04月23日 12:11:59 +00:00Commented Apr 23, 2019 at 12:11
-
$responseDecode['result']['0']['status'] Code is not workingShomita– Shomita2019年04月23日 12:12:33 +00:00Commented Apr 23, 2019 at 12:12
-
try
echo $responseDecode['result']['0']['status'];and check what you get. It should work as per info given abovefmsthird– fmsthird2019年04月23日 12:17:44 +00:00Commented Apr 23, 2019 at 12:17
You just need to decode your json data and then you can print value.
$array = json_decode($responseDecode); echo $array['result'][0]['status'];