dregduc63699 2012-06-26 06: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-26 06: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 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输