1

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; } } 
5
  • Loop array in foreach and overwrite key sdshan by ASDF Commented Apr 11, 2016 at 11:16
  • It's possible yes. Can you show us what you've tried ? Commented Apr 11, 2016 at 11:16
  • $object.sdshan = "ASDF"; Commented Apr 11, 2016 at 11:17
  • Good thoughts guys. Also what is the variable name of your array. Or do you have some example code? Commented Apr 11, 2016 at 11:17
  • im trying to output data from database by double queries it. im querying from table A, then im querying again from the results to table B. I just replace the field from result Array B to result Array A. Commented Apr 11, 2016 at 11:22

3 Answers 3

2

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"; } 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to apply foreach loop.

<?php $your_array = array(); foreach ($your_array as $array) { $pos = array_search($valToReplace, $array ); if ($pos !== FALSE) { $array[$pos] = $newVal; } } ?> 

Comments

1

Try this!

foreach(myarray as $key => $myobject){ $myobject->sdshan = "XXXX"; } 

to use object you have to use -> not .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.