Codeigniter替换模型中的数组值 [英] codeigniter replace array values in model
问题描述
我一直在绕着圈子替换多维数组.
I have been going around in circles with a multidimensional array replace..
我需要替换存储在与状态类型相关的数据库中的数字.如果我在视图中执行此操作,那可以工作,但似乎无法在模型中替换?
I need to replace numbers stored in a DB that relate to a status type.. If I did this in the view it would work but it wont seem to replace in the model?
function fetch_shipments($orgID){
$this->db->select('shipID, shipRef, shipCarrier, shipOrigin, shipDestination, shipQty, shipStatus');
$this->db->where('orgID', $orgID);
$query = $this->db->get('shipments');
$result = $query->result_array();
foreach($result as $row) {
foreach ($row as $key => $val) {
$key == 'shipStatus' && $val == 0 ? $val = 'Current' : $val;
$key == 'shipStatus' && $val == 1 ? $val = 'Pending' : $val;
$key == 'shipStatus' && $val == 2 ? $val = 'Complete' : $val;
}
}
return $result;
}
真的让我挠头了,我知道这种foreach一直在使用,因为我一直在使用它……我觉得我错过了一些东西(也许很明显),但我只是不能把手放在上面.在输出数组之前,我什至尝试在对象级别进行替换,但也无法正常工作.
Has really left me scratching my head, I know this kind of foreach works as I use it all the time... I feel I am missing something (perhaps obvious) but I just cant put my finger on it. I even tried doing the replace at the object level before outputting an array but couldn't get that to work either.
推荐答案
我相信在循环变量之前放置&
应该可以解决您的问题,因为引用将得到更新.
I believe placing a &
before your loop variable should solve your problem, as then the reference would get updated.
所以您的循环将开始这样
So your loop would start something like this
foreach($ result as& $ row){foreach($ row as $ key =>& $ val){
这篇关于Codeigniter替换模型中的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!