打印出类型的对象一个PHP数组 [英] Printing out a PHP Array of type object
本文介绍了打印出类型的对象一个PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组,看起来像这样:
I have an array that looks like this:
Array
(
[0] => stdClass Object
(
[user_id] => 10
[date_modified] => 2010-07-25 01:51:48
)
[1] => stdClass Object
(
[user_id] => 16
[date_modified] => 2010-07-26 14:37:24
)
[2] => stdClass Object
(
[user_id] => 27
[date_modified] => 2010-07-26 16:49:17
)
[3] => stdClass Object
(
[user_id] => 79
[date_modified] => 2010-08-08 18:53:20
)
)
和我需要做的就是打印出来的用户ID的逗号分隔这样:
and what I need to do is print out the user id's comma seperated so:
10,16,27,79
10, 16, 27, 79
我猜它会是一个循环,但我正在寻找最有效的方式做到这一点在PHP
I'm guessing it'd be in a for loop but i'm looking for the most efficient way to do it in PHP
呵呵和数组名称是:$ MARRAY
Oh and the Array name is: $mArray
我试过这样:
foreach($mArray as $k => $cur)
{
echo $cur['user_id'];
echo ',';
}
而其他建议。
不过,我不断收到此错误:
However I keep getting this error:
致命错误:无法使用类型stdClass的对象在阵列
Fatal error: Cannot use object of type stdClass as array in.
我想这是因为这个数组是不是一个典型的数组,因此需要一些不同的语法?
I think it's because this array is not a typical array so it requires some different syntax?
推荐答案
使用这个,如果你想避免后面的逗号(,
)。
Use this if you want to avoid the trailing comma (,
).
$ids = array();
foreach ($array as $obj){
$ids[] = $obj->user_id;
}
echo join(', ', $ids);
这篇关于打印出类型的对象一个PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文