duanhuanzhi6431 2016-04-11 11:15
浏览 75
已采纳

如何在PHP中获取对象名称

Is it possible to change value of object ..?

I have this object in Array :

Array ( 
 [0] => stdClass Object ( 
  [**sdshan**] => 25014408 
  [sduom] => Z5
  [sduorg] => 11651 
 ) 
 [1] => stdClass Object ( 
  [**sdshan**] => 2500880
  [sduom] => Z5 
  [sduorg] => 11193 
 ) 
 [2] => stdClass Object ( 
  [**sdshan**] => 25014447 
  [sduom] => Z5 
  [sduorg] => 5000 
 ) 
 [3] => stdClass Object (
  [**sdshan**] => 2500557 
  [sduom] => Z5 
  [sduorg] => 4200 
 ) 
 [4] => stdClass Object ( 
  [**sdshan**] => 2500348 
  [sduom] => Z5 
  [sduorg] => 4000 
 ) 
)

And i want to change the values of sdshan into ASDF. So, the finall Array look like this :

Array ( [0] => stdClass Object ( 
 [**sdshan**] => ASDF 
 [sduom] => Z5
 [sduorg] => 11651 
) 
[1] => stdClass Object ( 
 [**sdshan**] => ASDF
 [sduom] => Z5 
 [sduorg] => 11193 
) 
[2] => stdClass Object ( 
 [**sdshan**] =>ASDF
 [sduom] => Z5 
 [sduorg] => 5000 
) 
[3] => stdClass Object (
 [**sdshan**] => ASDF 
 [sduom] => Z5 
 [sduorg] => 4200 
) 
[4] => stdClass Object ( 
 [**sdshan**] => ASDF 
 [sduom] => Z5 
 [sduorg] => 4000 
)
)

Updated, this is what i want try to do :

function test(){
    $result = $this->db->select('sdshan,sduom,sduorg')->from('a')->get();
    foreach($result->result() as $data){
       // i want to give some condition here, if sdshan then the values of object sdhsan will be change to ASDF
        $new_result[]=$data; 
    }
}
  • 写回答

3条回答 默认 最新

  • dongwuqi4243 2016-04-11 11:17
    关注

    Yes you can do it.

    You can loop through the array and set the value of object by it's key. Like this.

    for($i=0;$i<sizeof($array);$i++) {
             $object=$array[$i];
             $object->sdshan="your value";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方