PHP:修剪对象中的每个元素,如果为空,则设置为 N/A
我有一个对象:
stdClass Object
(
[Color] => Red
[Shape] => Round
[Taste] => Sweet
)
我想修剪对象中的每个元素,如果该元素为空,则将其设置为“N/A”
所以这个对象:
stdClass Object
(
[Color] => Red
[Shape] =>
[Taste] => Sweet
)
将变成这样:
stdClass Object
(
[Color] => Red
[Shape] => N/A
[Taste] => Sweet
)
我应该如何完成这个,也许是 array_walk?
I have an object:
stdClass Object
(
[Color] => Red
[Shape] => Round
[Taste] => Sweet
)
I want to trim each of the elements in the object and if that element is empty, set it to 'N/A'
So this object:
stdClass Object
(
[Color] => Red
[Shape] =>
[Taste] => Sweet
)
Would become this:
stdClass Object
(
[Color] => Red
[Shape] => N/A
[Taste] => Sweet
)
How should I accomplish this, array_walk maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们保持简单:
这将给出:
Let's keep it simple:
And that would give:
这是一个更复杂(且速度更慢)的方法,它允许您迭代对象的所有属性,而不管可见性如何。这需要 PHP5.3:
但是如果所有对象属性都是公共的,则无需使用它。
Here is a more sophisticated (and slower) one that would allow you to iterate over all properties of an object, regardless of Visibility. This requires PHP5.3:
But there is no need to use this if all your object properties are public.