循环数组对象 php,PHP循环遍历stdClass对象的数组
我有一個在MySQL中運行的查詢,它返回一個結果作為stdClass對象,如下所示:
array(8){
[
0
]=>object(stdClass)#36(1){
[
"color"
]=>string(7)"#a0a0a0"
}[
1
]=>object(stdClass)#35(1){
[
"color"
]=>string(7)"#e0e0e0"
}[
2
]=>object(stdClass)#30(1){
[
"color"
]=>string(7)"#f0f0f0"
}[
3
]=>object(stdClass)#37(1){
[
"color"
]=>string(7)"#f0f0f1"
}[
4
]=>object(stdClass)#34(1){
[
"color"
]=>string(7)"#404040"
}[
5
]=>object(stdClass)#38(1){
[
"color"
]=>string(7)"#c0c0c0"
}[
6
]=>object(stdClass)#39(1){
[
"color"
]=>string(7)"#e06080"
}[
7
]=>object(stdClass)#40(1){
[
"color"
]=>string(7)"#e06082"
}
}
我想得到顏色值.如何遍歷此對象并將每個十六進制顏色存儲在數組中?
解決方法:
很容易.遍歷數組并訪問對象和顏色屬性并將其分配給新的數組元素:
foreach($array as $object) {
$colors[] = $object->color;
}
標簽:php,arrays,stdclass
來源: https://codeday.me/bug/20190612/1225610.html
總結
以上是生活随笔為你收集整理的循环数组对象 php,PHP循环遍历stdClass对象的数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做了绝育手术还能做试管婴儿吗?
- 下一篇: csgo语音按键是哪个