本文介绍了如何使用内爆stdClass对象数组中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个stdClass对象的数组,我想用所有这些stdClass对象的一个​​特定字段构建一个逗号分隔的列表.我的数组如下所示:

I have an array of stdClass objects and I want to build a comma separated list using one specific field of all those stdClass objects. My array looks like this:

$obj1 = stdClass Object ( [foo] => 4 [bar] => 8 [foo-bar] => 15 );
$obj2 = stdClass Object ( [foo] => 16 [bar] => 23 [foo-bar] => 42 );
$obj3 = stdClass Object ( [foo] => 76 [bar] => 79 [foo-bar] => 83 );

$a = array(1=>$obj1 , 2=>$obj2 , 3=>$obj3);

我想内插到该数组中所有stdClass对象的foo上,以创建一个用逗号分隔的列表.因此,理想的结果是:

And I want to implode on foo of all the stdClass objects in that array to create a comma separated list. So the desired result is:

4,16,76

有什么方法可以通过爆破(或其他神秘函数)来完成此操作,而不必将此对象数组置于循环中?

Is there any way to do this with implode (or some other mystery function) without having to put this array of objects through a loop?

推荐答案

您可以使用 array_map() implode() ...

You could use array_map() and implode()...

$a = array_map(function($obj) { return $obj->foo; }, 
               array(1=>$obj1 , 2=>$obj2 , 3=>$obj3));

$a = implode(", ", $a);

这篇关于如何使用内爆stdClass对象数组中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:50