douxie5176 2017-07-18 05:36
浏览 57

PHP - 按给定值进行多维数组过滤

Array ( 
    [01755677011] => Array ( 

        [0] => Array ( 

            [0] => 1 
            [id] => 1 
            [1] => 01755677011 
            [phone_num] => 01755677011 
            [2] => NoAnswer 
            [status] => Answer 
        ) 
        [1] => Array ( 

            [0] => 2 
            [id] => 2 
            [1] => 01755677011 
            [phone_num] => 01755677011 
            [2] => NoAnswer 
            [status] => NoAnswer 
        ) 
        [2] => Array ( 

            [0] => 3 
            [id] => 3 
            [1] => 01755677011 
            [phone_num] => 01755677011 
            [2] => Answer 
            [status] => NoAnswer 
        ) 
        [3] => Array ( 

            [0] => 4 
            [id] => 4 
            [1] => 01755677011 
            [phone_num] => 01755677011 
            [2] => Answer 
            [status] => Answer 
        )
    )
    [01755677012] => Array (

        [0] => Array ( 

            [0] => 16 
            [id] => 16 
            [1] => 01755677012 
            [phone_num] => 01755677012 
            [2] =>No Answer 
            [status] => NoAnswer 
        ) 
        [1] => Array ( 

            [0] => 18 
            [id] => 18 
            [1] => 01755677012 
            [phone_num] => 01755677012 
            [2] => Answer 
            [status] => Answer
        ) 
    ) 
)

This is my current array. I want to filter only Answer status by phone_num.
Outpout Will Like this:

Phone Num   No of answer  
01755677011 0,23  
01755677012 1  

Can anyone helo me please ??

  • 写回答

1条回答 默认 最新

  • doutang1873 2017-07-18 08:33
    关注

    I do not understand how you get 0,23 for 01755677011. If you are displaying phone_num and the number of times it has "Answer" status, you can do it this way:

    $result = array();
    foreach ($num as $key => $row) {
        $result[$key] = array();
        $count = 0;
        foreach($row as $key_value => $row_value) {
            if ($row_value['status'] == 'Answer') {
                // $key is the key 01755677011 and 01755677012
                $result[$key]['id'][$count] = $row_value['id'];
                $count++;
            }
        }
    }
    
    if (!empty($result)) {
        echo "Phone Num | No of answer<br />";
        foreach ($result as $key => $row) {
            echo $key . " " . count($row['id']) . "<br />";        
        }
    }
    

    Second version:

    1) Based on "Answer" in either [status] or [2].

    2) Display array index of the corresponding phone_num.

    $result = array();
    foreach ($num as $key => $row) {
        $result[$key] = array();
        $count = 0;
        foreach($row as $key_value => $row_value) {
            if ($row_value['2'] == 'Answer' || $row_value['status'] == 'Answer') {
                // $key is the key 01755677011 and 01755677012
                $result[$key][$count]['index'] = $key_value;
                $count++;
            }
        }
    }
    
    print_r($result);
    
    if (!empty($result)) {
        echo "Phone Num | No of answer<br />";
        foreach ($result as $key => $row) {
            echo $key . " | ";
            if (!empty($row)) {
                foreach ($row as $data_key => $data) {
                    echo $data['index'];
                }
            } else {
                echo "No record";
            }
            echo "<br />"; 
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取