问题
In my JavaScript file I create this array:
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
[object Object]
So, how can i print in PHP an array of object? thanks
回答1:
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. I just implemented a small code with html for you.
<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.
来源:https://stackoverflow.com/questions/60884059/how-to-print-an-array-of-object-in-php