Home>

I have JavaScript code that passes decoded JSON to another function.
I want to reproduce this in PHP.
I tried to find it, but I didn't understand it.
Thanks for your guidance and encouragement.

for (var i in json) {
    message = message + json [i] .account.name + "\ n```" + json [i] .body + "` `` \ n ";
    postMessage (message);
}
// Source: https://www.cg-method.com/entry/2017/03/06/080000/
  • Answer # 1

    You may want to imitate the for-in grammar as it is, but
    Enter the town and follow the town,
    I think it should be used if there is a more convenient grammar.

    Assuming that a certain data structure is processed one by one from the beginning to the end,
    Then it is the idea that foreach is good.
    If you want to be aware of what number of data is being processed,
    Assuming that the arguments are 0,1,2, and so on, using the count function is a hand.


    When decoding a json string with the json_decode function,
    You can choose between objects and arrays.

    I thought that I should handle it in a foreach loop.

    Sample json data was not shown, so prepare sample data without permission,
    I wrote a sample code.

    Example of decoding into an object:

    name. "\ n```". $item->gender. "` `` \ n ";
    }
    echo $message;

    Example of decoding into an array:

    Example for for instead of foreach: