2019-08-16 14:02:45 2477次浏览 2条回答 0 悬赏 30 金钱

查询结果正常返回

array(8) {
  [0]=>
  object(SubjectTag)#119 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(1) "8"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(12) "公益行动"
      ["sort"]=>
      string(1) "4"
      ["status"]=>
      string(1) "1"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1532316235"
      ["updated_at"]=>
      string(10) "1541556833"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(1) "8"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
  [1]=>
  object(SubjectTag)#120 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(1) "9"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(12) "公益报名"
      ["sort"]=>
      string(1) "3"
      ["status"]=>
      string(1) "1"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1532316251"
      ["updated_at"]=>
      string(10) "1541556829"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(1) "9"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
  [2]=>
  object(SubjectTag)#121 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(2) "10"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(12) "公益组织"
      ["sort"]=>
      string(1) "2"
      ["status"]=>
      string(1) "1"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1532316260"
      ["updated_at"]=>
      string(10) "1532401686"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(2) "10"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
  [3]=>
  object(SubjectTag)#122 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(2) "16"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(15) "十佳志愿者"
      ["sort"]=>
      string(1) "1"
      ["status"]=>
      string(1) "1"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1541556615"
      ["updated_at"]=>
      string(10) "1541556818"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(2) "16"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
  [4]=>
  object(SubjectTag)#123 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(2) "11"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(12) "儿童服务"
      ["sort"]=>
      string(1) "4"
      ["status"]=>
      string(1) "0"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1532316278"
      ["updated_at"]=>
      string(1) "0"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(2) "11"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
  [5]=>
  object(SubjectTag)#124 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(2) "12"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(12) "敬老服务"
      ["sort"]=>
      string(1) "3"
      ["status"]=>
      string(1) "0"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1532316288"
      ["updated_at"]=>
      string(10) "1532316705"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(2) "12"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
  [6]=>
  object(SubjectTag)#125 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(2) "13"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(12) "应急救援"
      ["sort"]=>
      string(1) "2"
      ["status"]=>
      string(1) "0"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1532316296"
      ["updated_at"]=>
      string(10) "1532316702"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(2) "13"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
  [7]=>
  object(SubjectTag)#126 (11) {
    ["_new":"CActiveRecord":private]=>
    bool(false)
    ["_attributes":"CActiveRecord":private]=>
    array(9) {
      ["id"]=>
      string(2) "14"
      ["sid"]=>
      string(1) "3"
      ["name"]=>
      string(12) "流浪动物"
      ["sort"]=>
      string(1) "1"
      ["status"]=>
      string(1) "0"
      ["rel_sid"]=>
      string(1) "0"
      ["tag_type"]=>
      string(1) "0"
      ["created_at"]=>
      string(10) "1532316305"
      ["updated_at"]=>
      string(10) "1532316762"
    }
    ["_related":"CActiveRecord":private]=>
    array(0) {
    }
    ["_c":"CActiveRecord":private]=>
    NULL
    ["_pk":"CActiveRecord":private]=>
    string(2) "14"
    ["_alias":"CActiveRecord":private]=>
    string(1) "t"
    ["_errors":"CModel":private]=>
    array(0) {
    }
    ["_validators":"CModel":private]=>
    NULL
    ["_scenario":"CModel":private]=>
    string(6) "update"
    ["_e":"CComponent":private]=>
    NULL
    ["_m":"CComponent":private]=>
    NULL
  }
}
$status = array_column($result, 'status');

使用 array_columnstatus 未能正确返回,结果为空数组

array(0) {
}

有大佬能指教下吗,感谢!

最佳答案

  • gouguoyin 发布于 2019-08-16 14:45 举报

    用活动记录查询时,将查询结果转换为数组后再使用array_column()函数,如
    $result = Customer::find()->asArray()->all();
    array_column($result,'status')
    如果只是查询部分字段,可以配合select()和column()使用,如
    $result = Customer::find()->select('status')->column();

    1 条回复
    回复于 2019-08-16 14:57 回复

    感谢,受教

  • 回答于 2019-08-16 19:08 举报

    还可以使用 ArrayHelper 类

    $status = ArrayHelper::getColumn($result, 'status'); 
    
您需要登录后才可以回答。登录 | 立即注册
Simine
经理

Simine 江苏省

注册时间:2019-04-18
最后登录:2020-03-14
在线时长:13小时44分
  • 粉丝0
  • 金钱995
  • 威望0
  • 积分1125

热门问题