doushu9253 2015-06-19 08:02
浏览 49
已采纳

php mysql_fetch_array中的implode数组

I cannot solve this seeming simple problem. I have the following simple code and all I want is to echo the result of $ATL5_Alert_query and separated by a comma (,):

$ATL5_Alert_query = mysql_query("SELECT `Mobile` FROM `dbo_tech_name` WHERE `AlertLevel`= 1"); 

$dataset = array();
 while ($data = mysql_fetch_array($ATL5_Alert_query))
 {
   $dataset[] = $data;
 }
echo implode (",", $dataset);

However, I'm getting "Notice: Array to string conversion "...

  • 写回答

1条回答 默认 最新

  • duanfengshang1088 2015-06-19 08:04
    关注

    In your code $data is array as well, so $dataset becomes an array of arrays, which you cannot concatenate. You should get the searched value by this:

    while ($data = mysql_fetch_array($ATL5_Alert_query))
    {
       $dataset[] = $data['Mobile'];
    }
    

    or:

    while ($data = mysql_fetch_array($ATL5_Alert_query))
    {
       $dataset[] = $data[0];
    }
    

    or:

    while ($data = mysql_fetch_assoc($ATL5_Alert_query))
    {
       $dataset[] = $data['Mobile'];
    }
    

    If you however cannot change this, and already have your $dataset array, you can implode it like that:

    echo implode(',', array_map(function($a){
      return $a['Mobile'];
    },$dataset));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭