Welcome Guest, Not a member yet? Register   Sign In
[Problem] Store stdclass to result?
#1

I have a code for get result of a table
And i need change some value in result to different and store it to new array, but it's impossible

My code same

PHP Code:
function test($id) {
            
$this->db->where("id"$id);
            
$rs $this->get('customer')->result();
            
$rs_new = array();
            foreach(
$rs as $cust) {
                
$cust->status '2';
                
$rs_new [] = $cust;
            }
            return 
$rs_new;        
        } 

When i using var_dump to check the $rs_new it same $rs, this sound mean the variable $rs_new not stored the new value of $cust
But, when i using var_dump($cust) in foreach loop so the status change for variable $cust

How can i do it for store?

Thanks you for read!
Reply
#2

Your code looks correct. Try to dump the object and the array in your function and just for testing add a new property to the object.

PHP Code:
function test($id) {
            
$this->db->where("id"$id);
            
$rs $this->get('customer')->result();
            
$rs_new = array();
               
            
print_r($rs);

            foreach(
$rs as $cust) {
                
$cust->status '2';
                
$cust->debuging 'just a test';
                
$rs_new [] = $cust;
            }

            
print_r($rs_new);

            return 
$rs_new;        
        } 

Are the dumps the same ?

Reply
#3

(This post was last modified: 11-27-2014, 01:16 AM by slax0r.)

That's because objects in PHP can not be dereferenced. At least not without a workaround.
Example:
Code:
$obj1 = new stdClass();
$obj1->test = 1;
$obj2 = $obj1;
$obj2->test = 2;
var_dump($obj1, $obj2); //will print both ->test values int(2)

The workaround would be to cast object to array when copying and then casting the copied array to object.

So I would suggest using arrays, but if you absolutely wish to keep it as an object:
Code:
function test($id) {
            $this->db->where("id", $id);
            $rs = $this->get('customer')->result_array();
            $rs_new = array();
            foreach($rs as $cust) {
                $cust['status'] = '2';
                $rs_new [] = (object)$cust;
            }
            return $rs_new;        
        }
Reply
#4

You can set a new value for a proterty in objects

PHP Code:
$obj1 = new stdClass();
$obj1->test 1;
var_dump($obj1);
$obj1->test 333;
var_dump($obj1);

$array = array("test" => 1"again" => 2);
$obj = (object) $array;
var_dump($obj);
$obj->test 333;
var_dump($obj); 

Reply
#5

I had short time and tested it like that

PHP Code:
$rs $this->db->get('table')->result();
print_r($rs);
$rs_new = array();
foreach(
$rs as $cust) {
    
$cust->title '*** new value ****';
    
$rs_new[] = $cust;
}
print_r($rs_new); 

It works as desired. You should check your call for the get()-Method:

PHP Code:
from
$rs 
$this->get('customer')->result_array();

to
$rs 
$this->db->get('customer')->result_array(); 

Reply
#6

(This post was last modified: 11-27-2014, 02:46 AM by slax0r.)

I did not say you can not set a property to an object. I only said objects can not be dereferenced in PHP without a workaround.
Example:
PHP Code:
$obj = new stdClass();
$obj->test 1;
$obj->stuff "test";
$arr = [
    
$obj
];

$new = [];
foreach (
$arr as $a) {
    
$a->test 2;
    
$new[] = $a;
}

var_dump($arr$new); 
Output:
Quote:$ php test.php
array(1) {
[0]=>
object(stdClass)#1 (2) {
["test"]=>
int(2)
["stuff"]=>
string(4) "test"
}
}
array(1) {
[0]=>
object(stdClass)#1 (2) {
["test"]=>
int(2)
["stuff"]=>
string(4) "test"
}
}

Foreach does exactly this:
PHP Code:
$a $arr[0]; 
Thus assigning the address of $arr[0] object to variable $a.
Reply
#7

@slax0r: you're right, but result() gives back an object witch you can iterate in a proper way.

Reply




Theme © iAndrew 2016 - Forum software by © MyBB