[英]How to print an Array of Object in PHP
In my JavaScript file I create this array:在我的 JavaScript 文件中,我创建了这个数组:
const get_rows = () => {
return document.getElementById("section-content").querySelectorAll('.element');
}
( get_rows() ).forEach( ( item, index ) => {
currentOrder[ item.id ] = index;
})
When I pass this array(currentOrder) in PHP by ajax, I try to print it, but it gives me当我通过 ajax 在 PHP 中传递这个数组(currentOrder)时,我尝试打印它,但它给了我
[object Object]
So, how can i print in PHP an array of object?那么,我如何在 PHP 中打印一个对象数组? thanks
谢谢
I am trying to give you answer, but because there is no proper explanation so it is quite difficult to understand the proper issue.我试图给你答案,但因为没有正确的解释,所以很难理解正确的问题。 What I find in your case is, you are not defined currentOrder.
我在您的情况下发现的是,您没有定义 currentOrder。 I just implemented a small code with html for you.
我刚刚为您实现了一个带有 html 的小代码。
<html> <body> <div id="section-content"> <div id="id1" class="element">hi</div> <div id="id2" class="element">hi1</div> <div id="id3" class="element">hi2</div> </div> <script> console.log("hi"); const get_rows = () => { return document.getElementById("section-content").querySelectorAll('.element'); } var currentOrder = []; ( get_rows() ).forEach( ( item, index ) => { currentOrder[item.id] = index; }) console.log(currentOrder); // Here you have to send currentOrder to your php code. </script> </body> </html>
Second thing is, why you are getting "index" in your array, Its just index of your element inside section-content.第二件事是,为什么要在数组中获取“索引”,它只是部分内容中元素的索引。 If still you have any problem please try to share more code snippets to help us to answer properly.
如果您仍有任何问题,请尝试分享更多代码片段以帮助我们正确回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.