dongyi2159 2013-12-31 09:36
浏览 38
已采纳

如何从php数组中删除元素[重复]

This question already has an answer here:

I'm getting this array as a result of mysql query, then i need to remove some data from this array after conditional statement

 `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" } }

`

my condition is

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.

          }
      }

  }
</div>
  • 写回答

3条回答 默认 最新

  • duanping3587 2013-12-31 09:41
    关注

    For cleaner code, use the array_filter function http://fr2.php.net/array_filter

    $result = array_filter($result, function($row) {
        return $row['Department'] != 'br' || $row['Br_no'] === $row['emp_servicing_bnk'];
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?