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 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了