When I tried to print it like this using the loop :
foreach($myJSON as $key => $value) {
echo "$key is at $value";
}
It didn’t print anything. Also, even if I manage to print it in my desired format, I don’t want to echo it but store all the result in maybe a variable or something so that I could pass it to a webservice using cURL.
Once you have run your object through JSON encoding, it comes out as a single string, which is what makes it useful for sending multiple data items through a medium where field separators and different data types are difficult to handle. So, you either need to use json_decode() to convert it back into the format you started with, or work through it yourself using explode() and so on. The latter option, though, is just writing your own version of json_decode() so probably easier to use the original.
Is that because $other is your original object, and you can’t just echo that out? You need to access it as you would normally access the object members. Can you do this?
I see. Using echo $other->id; printed the 123 which is the id. Then I am wondering should I be building a JSON in the format I mentioned above (containing name and value) using echo? Because here I am not at a point where I could loop an array and use foreach something.
These are three pairs of shoes. JSON is a data interchange format. ECHO is a command to push a string onto the stdout stream. LOOPs are flow control statements. They are not related.