为了账号安全,请及时绑定邮箱和手机立即绑定

有条件地从数组中删除一个元素 - Cakephp

有条件地从数组中删除一个元素 - Cakephp

PHP
qq_遁去的一_1 2022-10-28 14:51:17
您好,所有开发人员,我有下一个查询,根据我所在的国家/地区,我需要从所选字段中删除一个我不需要的元素,因为在某些国家/地区的数据库中,该特定列(字段)不存在。在其他国家/地区,查询必须保持初始状态。因此,查询将遵循以下结构:  $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 是否可行,对吗?提前致谢
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

最简单的方法是过滤数组:

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

基本上,它返回与您要消除的数组不匹配的任何值。


查看完整回答
反对 回复 2022-10-28
?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

实际上有很多方法可以使用现有的 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


查看完整回答
反对 回复 2022-10-28
  • 2 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信