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 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端