dtwxt88240 2017-04-09 12:11
浏览 19

仅显示数组中的第一个索引(PHP)

I am trying to loop and display all the contents of my array, but the problem is, when I use a counter, the code only returns the first letter of the first index word in the array and if I removed the counter, only the first index of the array is displayed.

This is my code:

$query = $this->chartsData->missingPerRegion();
$region_name = array();
$missing_num = array();

$count = 0;

foreach($query->result_array() as $row){
    $regionQuery = $this->chartsData->getRegions($row['affected-population_region_id']);

    foreach($regionQuery->result_array() as $region){
        $region_name = $region['name'];
    }

    $missing_num = $row['missing'];
}

while($count < count($region_name) && $count < count($missing_num)){
    echo '{"label": "' . $region_name[$count] . '", "value": ' . $missing_num[$count] . '},';
    // Outputs '{"label": "R", "value": 0},'
    // Expected Output: '{"label: "Region X", "value": 0}, {"label: "RegionXI", "value": 10}, ...'
    $count++;
}

Anyone who knows how to solve this problem?

  • 写回答

1条回答 默认 最新

  • dqrnsg6439 2017-04-09 12:14
    关注

    You are overwriting the region name that you store in the $region_name variable in every iteration of your inner foreach loop. You need to collect the individual region names instead:

     foreach ($regionQuery->result_array() as $region) {
        $region_name[] = $region['name'];
    }
    

    Notice the [] after the $region_name variable. This means that we are pushing each region name to the end of that array, instead of overwriting the variable in each iteration.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)