【问题标题】:how to remove elements from a php array [duplicate]如何从php数组中删除元素[重复]
【发布时间】:2014-01-18 06:11:34
【问题描述】:

我得到这个数组是mysql查询的结果,然后我需要在条件语句之后从这个数组中删除一些数据

 `array(5) { [0]=> array(13) 
{
 ["emp_name"]=> string(14) "Yasitha Perera"
 ["emp_applied_date"]=> string(10) "2013 11 27"
 ["emp_applied_time"]=> string(8) "12:12 pm" 
 ["hod_apprvd_time"]=> string(0) "" 
["hr_apprvd_time"]=> string(0) "" 
["emp_pending_on"]=> string(6) "BR/Hod" 
["emp_status"]=> string(0) "" 
["Br_no"]=> string(3) "001" 
["Br_name"]=> string(23) "1st Colombo City Branch"
 ["Contact_no"]=> string(23) "1st Colombo City Branch" 
["user_name"]=> string(3) "br1" 
["Hd_working_day"]=> string(1) "0" 
["Department"]=> string(2) "br" 
} 
[1]=> array(13) {

 ["emp_name"]=> string(14) "Yasitha Perera"
 ["emp_applied_date"]=> string(10) "2013 11 27" 
["emp_applied_time"]=> string(8) "12:12 pm"
 ["hod_apprvd_time"]=> string(0) "" 
["hr_apprvd_time"]=> string(0) "" 
["emp_pending_on"]=> string(6) "BR/Hod" 
["emp_status"]=> string(0) "" 
["Br_no"]=> string(3) "002" 
["Br_name"]=> string(18) "002-Baththaramulla" 
["Contact_no"]=> string(3) "br2" 
["user_name"]=> string(3) "br2"
 ["Hd_working_day"]=> string(1) "1"
 ["Department"]=> string(2) "br" } 
[2]=> array(13) { ["emp_name"]=> string(12) "emp_rols_abt"
 ["emp_applied_date"]=> string(10) "2013 12 13" 
["emp_applied_time"]=> string(8) "01:24 pm" 
["hod_apprvd_time"]=> string(8) "12:49 pm" 
["hr_apprvd_time"]=> string(8) "04:01 pm"
["emp_pending_on"]=> string(1) "-" 
["emp_status"]=> string(0) ""
 ["Br_no"]=> string(2) "hr" 
["Br_name"]=> string(7) "HR Dep." 
["Contact_no"]=> string(2) "hr" 
["user_name"]=> string(3) "hr1" 
["Hd_working_day"]=> string(1) "1"
["Department"]=> string(2) "hr" } 
[3]=> array(13) { 

["emp_name"]=> string(7) "my test"
 ["emp_applied_date"]=> string(10) "2013 12 13"
 ["emp_applied_time"]=> string(8) "01:27 pm"
 ["hod_apprvd_time"]=> string(0) ""
 ["hr_apprvd_time"]=> string(0) "" 
["emp_pending_on"]=> string(6) "BR/Hod" 
["emp_status"]=> string(0) "" 
["Br_no"]=> string(4) "icbs" 
["Br_name"]=> string(9) "ICBS Dep." 
["Contact_no"]=> string(5) "icbs1"
 ["user_name"]=> string(5) "icbs1" 
["Hd_working_day"]=> string(1) "1" 
["Department"]=> string(4) "icbs" }

[4]=> array(13) { ["emp_name"]=> string(3) "tae"
 ["emp_applied_date"]=> string(10) "2013 12 31"
["emp_applied_time"]=> string(8) "12:42 pm" 
["hod_apprvd_time"]=> string(0) "" 
["hr_apprvd_time"]=> string(0) "" 
["emp_pending_on"]=> string(6) "BR/Hod"
["emp_status"]=> string(0) "" 
["Br_no"]=> string(2) "it"
 ["Br_name"]=> string(7) "IT Dep." 
["Contact_no"]=> string(3) "it1"
["user_name"]=> string(3) "it1"
["Hd_working_day"]=> string(1) "0" 
["Department"]=> string(2) "it" } }

`

我的情况是

foreach ($data as $row){
      if($row['Department']=='br'){
          if($row['Br_no']!== $row['emp_servicing_bnk']){

              //here i need to remove the things that not acceptable by this condition.
// but i don't know how to remove it from the array.

          }
      }

  }

【问题讨论】:

  • 请使用 echo "
    ";显示数组。
  • @AttilaBujáki - 它没有解决我的问题

标签: php arrays


【解决方案1】:

您可以使用unset() 函数来销毁数组中的特定键:

foreach ($data as $key => $row) {
    if (/* your condition*/) {
        unset($data[$key]);
    }
}

【讨论】:

    【解决方案2】:

    要获得更简洁的代码,请使用 array_filter 函数 http://fr2.php.net/array_filter

    $result = array_filter($result, function($row) {
        return $row['Department'] != 'br' || $row['Br_no'] === $row['emp_servicing_bnk'];
    });
    

    【讨论】:

    • 谢谢埃洛伊姆斯先生。这很好用
    【解决方案3】:

    在 php 中使用 unset。类似于以下代码:

    unset($row['emp_servicing_bnk'])
    

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2011-07-03
      • 2012-02-12
      • 2016-12-08
      • 2021-10-01
      相关资源
      最近更新 更多