dongqie4233 2013-10-22 10:38
浏览 54
已采纳

用PHP返回数组的最后一个值

I have an array that is used later used by jquery to plot a chart (hence the json_encode).

<?php   
    $server = "myserver:1234";
    $user="dbuser";
    $password="userpass";  
    $database = "dbname";

    $connection = mysql_connect($server,$user,$password);
    $db = mysql_select_db($database,$connection);

    $query = "SELECT Y FROM listener_incr";

    $result = mysql_query($query);        

    $i = -60;

    while($row = mysql_fetch_assoc($result))
    {
        $dataset1[] = array($i,$row['Y']);
        $i++;
    }
    $final = json_encode($dataset1,JSON_NUMERIC_CHECK);    
?>  

The output of $final is [[-60,5],[-59,3],[-58,6],...[-1,7],[0,8]]

Aside from the data series being used to plot a chart, I want to echo the last value of my array to a div.

Here is what I have tried...

<div id="LastInArray">
  Current count: <?php
   end($dataset1);
   $intOutput = key($dataset1);
   echo $intOutput;
  ?>
</div>

But the above outputs Current count: 59, which I don't understand. The output I want for the div (in this example) is Current count: 8.

Later I want to use AJAX to update this value, which is why it's in a div.

  • 写回答

5条回答 默认 最新

  • du060334 2013-10-22 10:45
    关注

    end() doesn't traverse your multi-dimensional array iirc. For your value something like $dataset1[count($dataset1)-1][1] should be enough.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题