Conditionally delete an element from array - Cakephp

Oris Sin :

Hello there to all developers,

I have this next query and depending on which country I am located, I need to delete an element from the selected fields which I don't need as in the database of some countries, that specific column(field) doesn't exist. In the rest of the countries the query has to remain as it is inicially. Therefore, the query would follow this structure :

  $options['joins'] = array(
    array(
        'table' => 'td_addresses',
        'alias' => 'Address',
        'type' => 'LEFT',
        'conditions' => array(
            'Doctor.id = Address.doctor_id'
        )
    ),
    array(
        'table' => 'td_doctors_products',
        'alias' => 'DoctorsProducts',
        'type' => 'LEFT',
        'conditions' => array(
            'Doctor.id = DoctorsProducts.id_doctor'
        )
    ),
    array(
        'table' => 'td_products',
        'alias' => 'Products',
        'type' => 'LEFT',
        'conditions' => array(
            'DoctorsProducts.id_product = Products.id'
        )
    ),
    array(
        'table' => 'td_addresses_agenda',
        'alias' => 'AddressesAgenda',
        'type' => 'LEFT',
        'conditions' => array(
            'AddressesAgenda.address_id = Address.id'
        )
    )
);

$options['conditions'] = array(
    'Doctor.email !=' => '',
    'Doctor.accepted_social_politics >=' => 0
);

$options['fields'] = array(
 'Doctor.id', 
 'Doctor.email', 
 'Doctor.name',
 'Doctor.surname', 
 'Doctor.created', 
 'Doctor.profileimg', 
 'Doctor.list_experiencia_professional',
 'Doctor.logros_academicos', 
 'Doctor.premios_reconocimientos',
 'Doctor.status', 
 'Doctor.type_doctor',
 'Doctor.sexo',
 'Doctor.accepted_politics_wallet',
 'Doctor.accepted_social_politics',
 'DoctorsProducts.id_product', 
 'Address.phone',
 'Address.info_consulta',
 'DoctorsProducts.status',
 'AddressesAgenda.address_id');

echo json_encode($options['fields']["Doctor.accepted_politics_wallet"]);
$latam = ['mx', 'co'];
if(in_array(PAIS, $latam)){
    // Remove the field of Doctor.accepted_politics_wallet from $options['fields']
}


$options['order'] = array('Doctor.id ASC');
$options['group'] = array('Doctor.email');
$doctors_csv = $this->Doctor->find('all', $options);

Is this feasible with applicating an array_splice, right ?

Thanks in advance

Always Sunny :

Actually there are many ways to do it with existing php array function e.g array_filter(), array_diff(), array_search() etc. with array_search()

if (($key = array_search('Doctor.accepted_politics_wallet', $options['fields'])) !== false) {
    unset($options['fields'][$key]);
}
 print_r($options);

WORKING DEMO: https://3v4l.org/nkQZt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194358&siteId=1