-2
Array
(
    [id] => 7.8803211967429E+17
    [title] => Example T-Shirt
    [body_html] => 
    [variants] => Array
        (
            [0] => Array
                (
                    [id] => 6.4266704147271E+17
                )

            [1] => Array
                (
                    [id] => 7.576504846442E+17
                    [product_id] => 7.8803211967429E+17
                )

        )

    [options] => Array
        (
            [0] => Array
                (
                )

        )

)

I need to get IDs from each main array. I get this array from decoding json using:

<?php

    set_time_limit(0);
    $json = file_get_contents('imarasoft.net/imarasoft/abd/WebHook/response.txt');
    $arrays = json_decode($json, true);
    print_r($arrays);
    foreach ($arrays as $array) {
        echo $array["id"];
    }

How can I print ID using foreach? i HAVE EDITED THE QUESTION AND HAVE GIVEN THE FULL ARRAY FORMAT

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
Abdul Rahman
  • 1,891
  • 1
  • 9
  • 11

1 Answers1

1

If you are getting more number of elements in the array then you can use below snippets. This foreach loop go through all the arrays and gives value of each array's key. Here you can any value like title, id, vendor, product_type etc...

If you want access id inside variants then use below code.

$json = file_get_contents('http://imarasoft.net/imarasoft/abd/WebHook/response.txt');
$arrays = json_decode($json, true);
foreach ($arrays["variants"] as $array) {
    echo $array["id"];
}

And if you only want to access root id then you just need to do only one line snippet.

echo $arrays["id"];
Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47