I am getting this error "Trying to get property of non-object" for lines
'price' => $product->product_price,
'name' => $product->product_name
for doing in the same page
function remove($rowid) {
$this->cart->update(array(
'rowid' => $rowid,
'qty' => 0
));
}
i can solve this problem, by doing like, 'price' => $product['product_price'],
Bus as my other page using 'price' => $product->product_price
them as fine, so i dont want to convert it to array, my question is how can i convert $this->cart->update(array(
to an object so that, these lines
'price' => $product->product_price,
'name' => $product->product_name
works fine for object?
Thanks in advance.
$Arr = array(
'rowid' => $rowid,
'qty' => 0
);
$Obj = (object)$Arr;
This is conversion of Array to Object. Do you want it?
You assign the result to an array. Then you try to use the array as an object. Next, the returned result is an array as well which you try to use as a object.
The smarter ways you can convert array into object
From PHP manual:
<?php
$literalObjectDeclared = (object) array(
'foo' => (object) array(
'bar' => 'baz',
'pax' => 'vax'
),
'moo' => 'ui'
);
print $literalObjectDeclared->foo->bar; // outputs "baz"!
?>
little genius hack:
<?php
// assuming $var is a multidimensional array
$obj = json_decode (json_encode ($var), FALSE);
?>
So you can convert by casting array into object:
<?php
$array = array(
// ...
);
$object = (object) $array;
?>
if you want manual way you can do this:
<?php
$object = object;
foreach($arr as $key => $value)
{
$object->{$key} = $value;
}
?>
Define $product as a new standard class:
$product = new stdClass();