dregduc63699 2012-06-25 22:33
浏览 36
已采纳

php基础:mulitdimensional数组中的array_sum

I am doing the exercise in Learning PHP5 from Oreilly and the task is to caculate to total population of states (with a limited number of cities).

I have created a multidimensional array with states and then inside cities but unlike what is proposed as solution in the book, I am trying to use the sum_array() function to calculate, for each state, the population.

My logic must be somehow wrong as I array_sum doesn't seem to know which array to use (I triple-checked the names and I wrote the good array name). What am I missing?

Here's what I have so far:

$population = array ( 
    'NY' => array('New York' => 8008278),
    'CA' => array('Los Angeles' => 3694820, 'San Diego' => 1223400),
    'IL' => array('Chicago' => 2896016),
    'TX' => array('Houston' => 1953631, 'Dallas' => 1188580, 'San Antonio' => 1144646),
    'PA' => array('Philadelphia' => 1517550),
    'AZ' => array('Phoenix' => 1321045),
    'MI' => array('Detroit' => 951270)
);

print '<table><tr><th>State</th><th>City</th><th>Population</th></tr>';

foreach ($population as $state => $city_info) {
    foreach ($city_info as $city_name => $city_population) {
    print "<tr><td>$state</td><td>$city_name</td><td>$city_population</td></tr>";
    }
    print "<tr><td></td><td>{$state}'s total population</td><td>array_sum($city_info)</td></tr>";
}

print "<tr><td></td><td></td></tr>";
  • 写回答

1条回答 默认 最新

  • drk49438 2012-06-25 22:39
    关注

    You forgot about function inside quotes. Must be something like this.

    print "<tr><td></td><td>{$state}'s total population</td><td>".array_sum($city_info)."</td></tr>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧