Hi,i have the following code,how can i count values -averages per row and column from this code:

 <?php
$data = array( 
               array(""=>"January", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"February", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"March", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"April", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"May", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"June", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"July", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"August", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"September", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"October", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"November", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),
               array(""=>"December", "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)),


             ); 


?>

      <br> 




<?php if (count($data) > 0): ?>
    <div class="container">
        <style type="text/css">
table { width: 50%; }
</style>
<table  BORDER=1 BORDERCOLOR=green>
  <thead>
    <tr bgcolor= GREEN>
      <th ><?php echo implode('</th><th>', array_keys(current($data))); ?></th>

    </tr>
  </thead>
  <tbody>
<?php foreach ($data as $row): array_map('htmlentities', $row); ?>
    <tr>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>
        </div>
<?php endif; ?>

What exactly do you want to count?

I would like to compute average per month (per raw) and columns average

Just add an array keeping the sum of all your columns. Add the values inside the loop, divide and display after the loop.

Member Avatar for diafol

WHy have you got a blank key for months?

RE: To diafol

that field is blank,a nd monyhs ander in column,but i can put month in the field.The problem is the nestred arrays my weak point,i need to see exmple,to understand logic. I have seen example online,but,not with the array like this.

Member Avatar for diafol

You're probably better off with a simpler array structure:

$data = [
    'January'=> 
               [ "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)],
   'February' =>
               ["ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)],
...etc...
];

Alternatively, you can get rid of the month entirely, as arrays should be indexed 0-11 (Jan-Dec) :

$data = [
    [ "ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)],
   ["ATL"=>rand(0,335) , "ORD"=>rand(0,335),"LAX"=>rand(0,335),"DFW"=>rand(0,335),"DEN"=>rand(0,335),"JFK"=>rand(0,335),"SFO"=>rand(0,335),"CLT"=>rand(0,335),"LAS"=>rand(0,335),"PHX"=>rand(0,335)],
...etc...
];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.