Answer the question
In order to leave comments, you need to log in
How to output unlimited nesting of elements in an array through PHP?
Actually. There is a JSON pulled through the VK API with all the user's messages. This JSON is decoded and the result is a multidimensional array. In this array, each specific array is one message. In the message, in addition to the text, there may be a forwarded message. In a forwarded message, messages that are still being forwarded (i.e., the nesting is actually infinite). And each message (both regular and forwarded) can have an attachment (or multiple attachments).
For example:
To access attachments in a normal message, you would use:
$arr[message_number]["attachments"][attachment_number_in_message][attachment_type][element_with_link_to_attachment]
For attachments in a forwarded message:
$arr[message_number]["fwd_messages"][forwarded_message_number]["attachments"][attachment_number_in_message][attachment_type][element_with_link_to_attachment]
If anything -- https://vk.com/dev/message.
You should get HTML with a hierarchical structure (a block with a message, inside of which is a block with a forwarded message, and inside this block there is another one; in each such block there is an image). In general, the representation that messages have on the site itself.
The problem is how to handle it all. If levels 1 or 2 - everything is simple. But if there can be an infinite number of these levels, this is already a problem.
How to implement this, given the possible unlimited nesting?
Answer the question
In order to leave comments, you need to log in
The problem is how to handle it all.
this is the working js code on the topic:
var fwdfun = function (fwds,level=1) {
try {
for(var i in fwds){
const fwd = fwds[i];
var qo = ">".repeat(level);
console.log(qo+fwd.num);
if(fwd.arr){
fwdfun(fwd.arr,level + 1);
}
}
}catch(error){
console.error(error);
}
}
fwdfun(obj);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question