加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP循环遍历stdClass对象的数组

发布时间:2020-12-13 17:47:07 所属栏目:PHP教程 来源:网络整理
导读:我有一个在 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){ [ "c
我有一个在 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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读