griff4594
griff4594

Reputation: 502

PHP Foreach Array building with nested array

This is in reference to a CRM I am building.

I am trying to get data from phone contacts in my database and then separate them by the hour of day then filter through the contacts by each hour and count them. This is all generated into an array which is then placed into a graph.

So the goal is to count the calls by hour per provider that the call came in on. I can do this just fine by the agent, but now I want it to generate by provider. Here is a screenshot for reference. The highlighted section is supposed to be separate lines but they are currently overlapping because the data is not correctly generated.

Screenshot

Here is the 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
    );      
}

Here is the output:

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)
      }
    }
  }
}

The problem is if you look at the output the second array is generating PinkADT and RedADT data in the data nested array. Each should have just it's own data. So RedADT should look like this:

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

I have been playing with this for 2 days and I cannot figure out why it is including PinkADT data with the RedADT data in the data array.

Upvotes: 0

Views: 110

Answers (2)

Mateo Torres
Mateo Torres

Reputation: 1625

i think the problem is in the inner foreach loop:

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
 );      

}

i would implement a filter function that takes the $hour_array and gives back the desired information, like this

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
 );      

}

EDIT: now I see the mistake, you should flush your array, because you are just adding to it in every iteration.

change

$total_calls_array[] = array(

to

$total_calls_array = array(

to make it look like this

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
 );      

}

Upvotes: 1

griff4594
griff4594

Reputation: 502

If I do this it works fine, but I need it in array format because I need to plot the locations on a graph:

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

But I need it in array format that I had set for the data to plot.

Upvotes: 0

Related Questions