如何在Codeigniter PHP

时间:2019-05-24 06:44:06

标签: php codeigniter

如何在PHP中删除stdClass对象?

控制器:

$detail['shipping'] = $this->Mymodel->Tcs_get_data('shipment_master'); 这是我的ARRAY

查看:

使用print_r($shipping);

Array ( [0] => stdClass Object ( [sc_id] => 1 [sc_amount_s] => 0 [sc_amount_e] => 4999 [sc_charges] => 150 [sc_status] => 0 [sc_delete] => 0 ) [1] => stdClass Object ( [sc_id] => 2 [sc_amount_s] => 5000 [sc_amount_e] => 9999 [sc_charges] => 100 [sc_status] => 0 [sc_delete] => 0 ) [2] => stdClass Object ( [sc_id] => 4 [sc_amount_s] => 10000 [sc_amount_e] => 99999 [sc_charges] => 0 [sc_status] => 0 [sc_delete] => 0 ) )

3 个答案:

答案 0 :(得分:0)

您可以在模型查询中使用result_array()代替result()或执行以下操作:

$shippingArray = [];
foreach ($shipping as $key => $value) {
    array_push($shippingArray, $value)
}
print_r($shippingArray);

通过将所有值放在另一个数组中

或者您可以在视图中使用当前数组对象,例如:

foreach ($shipping as $key => $value) {
    // Here you can use $value as you want..
    // echo '<tr><td>'. $value->sc_amount_e .'</td></tr>'; 
}

答案 1 :(得分:0)

您拥有的是对象的关联数组。

stdClass表示通过指针->访问的对象。

$shipping[0]->sc_id是如何访问数组中的stdClass对象,而在没有对象的关联数组中,访问方法是$shipping[0]['sc_id']

如果您希望使用纯数组,则需要坚持使用result_array()row_array()

为清楚起见,尽管您可能在stdClass中看到print_r,但它不是作为字符串“可见”,而仅表示它是一个对象。

答案 2 :(得分:0)

在控制器中编写此代码,

$temp = $this->Mymodel->Tcs_get_data('shipment_master');
$detail['shipping'] = json_decode(json_encode($temp), true); // true is for making it array