3

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;
}

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.

2 Answers 2

3

you should save it on your $result variable. not in $val

foreach($result as $k => $row) {
    foreach ($row as $key => $val) {
        $key == 'shipStatus' && $val == 0 ? $result[$k][$key] = 'Current' : $val;
        $key == 'shipStatus' && $val == 1 ? $result[$k][$key] = 'Pending' : $val;
        $key == 'shipStatus' && $val == 2 ? $result[$k][$key] = 'Complete' : $val;
    }
}

return $result;

Or you could remove the inner loop

foreach($result as $k => $row) {
    if($row['shipStatus']==0){
         $result[$k]['shipStatus'] = 'Current';
    }elseif($row['shipStatus']==1){
         $result[$k]['shipStatus'] = 'Pending';
    }else{
         $result[$k]['shipStatus'] = 'Complete';
    }
}

return $result;
Sign up to request clarification or add additional context in comments.

4 Comments

Your solution has worked, thanks. I didn't think assigning the key at the row level would work? being a multi-dimensional array?
Glad i helped. It will work if your giving $row it's reference which is your $result[$k]
Second approach is much cleaner, great!
I have ended up using the second approach based on logic but changed it to use the Ternary operator
0

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) {

3 Comments

so the & allows the array to pass the value back up to the original foreach?
Yes, you can say that, when using the & operator the original variable will get updated
I'm going to add this as the answer as it fixes the actual code problem as-is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.