使用嵌套数组的PHP Foreach数组构建

时间:2014-03-04 17:03:10

标签: php foreach multidimensional-array

这是关于我正在建立的CRM。

我正在尝试从我的数据库中的手机通讯录中获取数据,然后按小时将它们分开,然后按小时过滤联系人并计算它们。这全部生成为一个数组,然后放入一个图形中。

因此,我们的目标是按来自每个提供商的电话计算来电。我可以通过代理做到这一点,但现在我希望它由提供者生成。这是一个参考截图。突出显示的部分应该是单独的行,但它们当前是重叠的,因为数据未正确生成。

Screenshot

这是PHP:

$user_level = $_POST['user_level'];
$id = $_POST['id'];
date_default_timezone_set('America/Denver');
$date = date("m/d/Y");

$today_manager_plot = "SELECT * FROM contacts AS c INNER JOIN skills AS s ON s.skill_id=c.skill_id WHERE start_date='$date' AND s.type='0'";
$today_query_manager_plot = $db->query($today_manager_plot);
$rows = $today_query_manager_plot->num_rows;
$today_plot_array = array();
$plot_array = array();
$providers = array();

$t=0;
foreach($today_query_manager_plot as $p)    {
    $providers[$t]['skill_id'] = $p['skill_id'];
    $providers[$t]['skill_name'] = $p['skill_name'];
    $providers[$t]['vendor_color'] = $p['vendor_color'];
    $t++;
}
$provider_array = array_unique($providers, SORT_REGULAR);
$i=0;

foreach($today_query_manager_plot as $tq)   {
    if($tq['type'] == 0)    {
        $hour = date("H", strtotime($tq['start_time']));
        $today_plot_array[$i] = $hour;
        $i++;
    }
}

natsort($today_plot_array);
$hour_array = array_unique($today_plot_array, SORT_REGULAR);

function total_calls_by_provider($hour, $array, $skill_id)  {
    $i=0;
    foreach($array as $a)   {
        $high = strtotime($hour.":59:59");
        $low = strtotime($hour.":00:00");
        if(($low <= strtotime($a['start_time']) && $high >= strtotime($a['start_time'])) && $a['skill_id'] == $skill_id)    {
            $i++;
        }   
    }
    return $i;
}

foreach($provider_array as $pa) {
    foreach($hour_array as $h)  {
        $provider = total_calls_by_provider($h, $today_query_manager_plot, $pa['skill_id']);
        $total_calls_array[] = array(
            "x" => intval($h),
            "y" => intval($provider)
        );          
    }

    $plot_array[] = array(
        "color" => $pa['vendor_color'],
        "name" => $pa['skill_name'],
        "data" => $total_calls_array
    );      
}

这是输出:

Provider info:array(2) {
  [0]=>
  array(3) {
    ["color"]=>
    string(4) "Pink"
    ["name"]=>
    string(7) "PinkADT"
    ["data"]=>
    array(1) {
      [0]=>
      array(2) {
        ["x"]=>
        int(9)
        ["y"]=>
        int(2)
      }
    }
  }
  [1]=>
  array(3) {
    ["color"]=>
    string(3) "Red"
    ["name"]=>
    string(6) "RedADT"
    ["data"]=>
     array(2) {
      [0]=>
      array(2) {
        ["x"]=>
        int(9)
        ["y"]=>
        int(2)
      }
      [1]=>
       array(2) {
        ["x"]=>
        int(9)
        ["y"]=>
        int(1)
      }
    }
  }
}

问题是如果你查看输出,第二个数组在数据嵌套数组中生成PinkADT和RedADT数据。每个人都应该拥有自己的数据。所以RedADT应该是这样的:

  [1]=>
  array(3) {
    ["color"]=>
    string(3) "Red"
    ["name"]=>
    string(6) "RedADT"
    ["data"]=>
     array(1) {
      [0]=>
       array(2) {
        ["x"]=>
        int(9)
        ["y"]=>
        int(1)
      }
    }
  }

我已经玩了2天了,我无法弄清楚为什么在数据阵列中包含RedADT数据的PinkADT数据。

2 个答案:

答案 0 :(得分:1)

我认为问题出在内部的foreach循环中:

foreach($provider_array as $pa) {
    foreach($hour_array as $h)  {
        //this foreach should be related to the $pa provider only, 
        //it seems you are not filtering this array  
        $provider = total_calls_by_provider($h, $today_query_manager_plot, $pa['skill_id']);
        $total_calls_array[] = array(
            "x" => intval($h),
            "y" => intval($provider)
        );          
     } 

     $plot_array[] = array(
     "color" => $pa['vendor_color'],
     "name" => $pa['skill_name'],
     "data" => $total_calls_array
 );      

}

我会实现一个过滤函数,它接受$hour_array并返回所需的信息,比如

foreach($provider_array as $pa) {
    foreach(provider_filter($hour_array,$pa) as $h)  {
        $provider = total_calls_by_provider($h, $today_query_manager_plot, $pa['skill_id']);
        $total_calls_array[] = array(
            "x" => intval($h),
            "y" => intval($provider)
        );          
     } 

     $plot_array[] = array(
     "color" => $pa['vendor_color'],
     "name" => $pa['skill_name'],
     "data" => $total_calls_array
 );      

}

编辑:现在我看到了错误,你应该刷新你的数组,因为你只是在每次迭代中添加它。

更改

$total_calls_array[] = array(

$total_calls_array = array(

让它看起来像这样

foreach($provider_array as $pa) {
    $total_calls_array = array()
    foreach($hour_array as $h)  {
        $provider = total_calls_by_provider($h, $today_query_manager_plot, $pa['skill_id']);
        $total_calls_array[] = array(
            "x" => intval($h),
            "y" => intval($provider)
        );          
     } 

     $plot_array[] = array(
     "color" => $pa['vendor_color'],
     "name" => $pa['skill_name'],
     "data" => $total_calls_array
 );      

}

答案 1 :(得分:0)

如果我这样做它工作正常,但我需要它以数组格式,因为我需要在图表上绘制位置:

foreach($provider_array as $pa) {
    echo "<table>";
    echo "<tr><td colspan='2'>".$pa['skill_name']."</td></tr>";
    foreach($hour_array as $h)  {
        $provider = total_calls_by_provider($h, $today_query_manager_plot, $pa['skill_id']);
        $skill_id = $pa['skill_id'];
        $hour = date('g:i a',strtotime($h.':00'));
        echo "<tr><td>".$hour."</td><td>".$provider."</td></tr>";
    }
    echo "</table>";
}

但是我需要以数组格式设置数据来绘制。