0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

When I return the $options['fields'], it sorts the elements of my array,something that I am not actually needing but I think that your answer suffices as a solution for deleting that specific element. Thanks !
@OrisSin glad it helps you somehow, best of luck mate :), another example is here : arjunphp.com/remove-specific-value-array-using-php
1

The easiest way would be to filter the array:

$options['fields'] = array_filter($options['fields'], function ($option) {
    return $option !== 'Doctor.accepted_politics_wallet';
});

Basically, it returns any value of the array that does not match the one you want to eliminate.

1 Comment

Your approach is also functioning perfectly. Thanks for your help.

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.