dpmrakfbx820320638 2018-09-25 13:41
浏览 91
已采纳

PHP数组值为变量

I'm using the following piece of code to count the number of locations from a table:

$result = mysqli_query($conn,"SELECT location FROM table ORDER BY location");

$aloc_list = array();
while ($row = mysqli_fetch_array($result))
        {$aloc_list[] = $row['location'];
        }

print_r(array_count_values($aloc_list));

Which gives me the following array:

Array
(
    [CANADA] => 106
    [USA] => 547
    [MEXICO] => 93
    [GREAT_BRITAIN] => 111
    [EASTERN_EUROPE] => 227
    [RUSSIA] => 405
    [CHINA] => 341
    [INDIA] => 253
)

I have tried to 'extract' the array values and put them in a variable but nothing seems to work:

$canada = $aloc_list[0];
$canada = $aloc_list['CANADA'];
$canada = $aloc_list[0]['CANADA'];
$canada = $aloc_list[0];

I want to end up with a variable called $canada equal to 106, a variable called $usa equal to 547, etc.

How do I accomplish this?

  • 写回答

5条回答 默认 最新

  • dongzituo5530 2018-09-25 13:49
    关注

    You are trying to select the mentioned values from the original $aloc_list array - but they don't directly exist in there. They're the result of the call to array_count_values($aloc_list) - this produces a new array, and you need to read from that instead.

    For example:

    $counts = array_count_values($aloc_list);
    $canada = $counts['CANADA'];
    echo $canada; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程