亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有條件地從數組中刪除一個元素 - 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條經驗 獲得超7個贊

實際上有很多方法可以使用現有的 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 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號