有条件地从数组中删除一个元素 - Cakephp
Conditionally delete an element from array - Cakephp
各位开发者大家好,
我有下一个查询,根据我所在的国家/地区,我需要从所选字段中删除一个我不需要的元素,因为在某些国家/地区的数据库中,该特定列(字段)不需要'存在。
在其他国家/地区,查询必须保持原样。
因此,查询将遵循以下结构:
$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);
应用 array_splice 可行吗?
提前致谢
最简单的方法是 filter 数组:
$options['fields'] = array_filter($options['fields'], function ($option) {
return $option !== 'Doctor.accepted_politics_wallet';
});
基本上,它returns数组中与您要消除的值不匹配的任何值。
实际上有很多方法可以使用现有的 php 数组函数来实现,例如 array_filter()
、array_diff()
、array_search()
等 array_search()
if (($key = array_search('Doctor.accepted_politics_wallet', $options['fields'])) !== false) {
unset($options['fields'][$key]);
}
print_r($options);
工作演示: https://3v4l.org/nkQZt
各位开发者大家好,
我有下一个查询,根据我所在的国家/地区,我需要从所选字段中删除一个我不需要的元素,因为在某些国家/地区的数据库中,该特定列(字段)不需要'存在。 在其他国家/地区,查询必须保持原样。 因此,查询将遵循以下结构:
$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);
应用 array_splice 可行吗?
提前致谢
最简单的方法是 filter 数组:
$options['fields'] = array_filter($options['fields'], function ($option) {
return $option !== 'Doctor.accepted_politics_wallet';
});
基本上,它returns数组中与您要消除的值不匹配的任何值。
实际上有很多方法可以使用现有的 php 数组函数来实现,例如 array_filter()
、array_diff()
、array_search()
等 array_search()
if (($key = array_search('Doctor.accepted_politics_wallet', $options['fields'])) !== false) {
unset($options['fields'][$key]);
}
print_r($options);
工作演示: https://3v4l.org/nkQZt