-1

I have a variable $arr in PHP. It has the following data inside it.

This is my PHP Code.

    $data = array(
        'Request' => 'StockStatus',
        'merchant_id' => 'shipm8',
        'hash' => '09335f393d4155d9334ed61385712999'
        );

    $url = 'https://ship2you.com/ship2you/';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_PORT, 443);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    curl_close($ch);

    $arr = json_decode($result, true);

    foreach ($arr as $value) {
        echo $value['packagename'];
    }

enter image description here

I want to loop through it. How can I achieve it? I tried using foreach but it gives me error. Thanks in advance.

13
  • show how did you use foreach ? Commented Nov 21, 2016 at 8:09
  • Also post the exact error message. Commented Nov 21, 2016 at 8:10
  • 1
    Possible duplicate of How do I loop through JSON object Commented Nov 21, 2016 at 8:10
  • @Anant I have pasted my php code. Commented Nov 21, 2016 at 8:11
  • 2
    Come on, post the code! Now you added a snippet, but still not the foreach you mentioned... Commented Nov 21, 2016 at 8:11

3 Answers 3

1

You have to decode your CURL output string twice:-

$arr = json_decode(json_decode($result, true),true);

foreach ($arr as $value) {
    echo "<pre/>";print_r($value['packagename']);
}

Note:- @Xatenev mentioned the correct thing:-

The json has escaped quotes. When a json with escaped quotes is passed to json_decode() it only removes all the escaped sequences. When calling json_decode() again, it decodes it correctly

Sign up to request clarification or add additional context in comments.

5 Comments

Why do i have to do it twice?
@AliZia It seems that double-encoded or you can say nested encoded string comes from CURL request
I wish my API Provider would have mentioned it. Thankyou so much.
@Anant @AliZia The json has escaped quotes. When a json with escaped quotes is passed to json_decode() it only removes all the escaped sequences. When calling json_decode() again, it decodes it correctly.
@AliZia glad to help you. Cheers :):)
1
json_decode($json, true);

If the second parameter is true, it will return array. In case it is not what you are looking for, please show the code you have.

Comments

0
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);

$arr = json_decode($result);



foreach ($arr as $arr_item) {
   echo $arr_item->user_id;
}

Comments

Your Answer

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.