doq13207 2015-06-04 03:24
浏览 41
已采纳

如何在PHP中使用字符串调用数组

I would like to echo a bunch of arrays in numerical order, I tried using WHILE method but lacks the knowledge on how to combine strings to call a variable and get the value inside the array.

$ins1 = array ( 
              "select"  => array (  
                                   "1" => "1"
                                   ), 
              "note"  => array ( 
                                   "1" => "Message"
                                   )
              ); 

$ins2 = array ( 
              "select"  => array (  
                                   "1" => "2"
                                   ), 
              "note"  => array ( 
                                   "1" => "Sorry"
                                   )
              ); 


$count = 1;
while($count <= 2){
    $ins = '$ins'.$count;
    echo $ins["select"][$count] .' '. $ins["note"][$count].'<br>';
    $count++;
}

OUTPUT SHOULD BE:

1 Message
2 Sorry
  • 写回答

3条回答 默认 最新

  • duan0065626385 2015-06-04 03:46
    关注

    What you're looking for is "Variable variables", through which you can set the variable name dynamically; so to get what you want, change your code as the following:

    $count = 1;
    while($count <= 2){
        $ins = 'ins'.$count;
        $var = $$ins; // now your $var is either $ins1 or $ins2 :)
        echo $var["select"][1] .' '. $var["note"][1].'<br>';
        $count++;
    }
    

    The output would be:

    1 Message
    2 Sorry
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历