LogixMaster
LogixMaster

Reputation: 586

CakePHP/php - reformatting array results

I have the following array which I obtain using sql from cake..

array (size=2)
  0 => 
    array (size=2)
      'users' => 
        array (size=1)
          'user_status' => boolean false
      0 => 
        array (size=1)
          'user_count' => string '17' (length=2)
  1 => 
    array (size=2)
      'users' => 
        array (size=1)
          'user_status' => boolean true
      0 => 
        array (size=1)
          'user_count' => string '4' (length=1)

I have a flag field for active/not active users, which holds boolean value either, true or false. I woul like to iterate over that array and change the value of false to not active, and true to active.

I tried this but it doesn't work

foreach($results as $result){
        if($result['users']['user_status'] == false){
            $result['users']['user_status'] = 'not active';
        }else{
            $result['users']['user_status'] = 'active';
        }
    }

Any other way could do this?

Upvotes: 0

Views: 37

Answers (1)

cornelb
cornelb

Reputation: 6066

foreach($results as &$result) {
    if($result['users']['user_status'] === false){
        $result['users']['user_status'] = 'not active';
    } else {
        $result['users']['user_status'] = 'active';
    }
}

This way you are saving the values in the $results array

Upvotes: 2

Related Questions