How to print an Array of Object in PHP

扶醉桌前 提交于 2020-04-17 21:30:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!