douqiao1997 2016-03-04 20:10
浏览 33
已采纳

同时变量数组

I've searched through the stack for an answer but it seems that I cannot find a solution for the following code:

$servers = array();
while($row = mysql_fetch_array( $result ))
{
   $servers[] = $row['name'] => array('ip' => $row['ip'],'port' => $row['port'],'info' => $row['info'],'purpose' => $row['purpose']),
}

What I'm trying to achieve is fetch all results but I receive the following error:

syntax error, unexpected '=>' (T_DOUBLE_ARROW).

Making it like this gets only the last item:

while($row = mysql_fetch_array( $result )) {
$servers = array(
    $row['name'] => array('ip' => $row['ip'],'port' => $row['port'],'info' => $row['info'],'purpose' => $row['purpose']),
    );
}

That being said, $servers is in the while loop, thus making it index only the last item. I don't know what causes the unexpected => :/

  • 写回答

1条回答 默认 最新

  • dongye8110 2016-03-04 20:13
    关注

    It looks like you're trying to do this:

    $servers = [];
    while($row = mysql_fetch_array( $result )) {
       $servers[$row['name']] =  [
           'ip' => $row['ip'],
           'port' => $row['port'],
           'info' => $row['info'],
           'purpose' => $row['purpose']
       ];
    }
    

    That gives you a final $servers array, indexed by server name, with an array of details.

    Side note: as Jay commented, you really shouldn't be using mysql functions any longer, really encourage you to check out mysqli or PDO functions instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!