数组中的最后一条记录返回null

时间:2017-10-04 07:23:34

标签: php arrays

我正在为电梯和那里的人写一个PHP练习脚本,这并不重要,因为一切都准备好了 - 但是在任务结束时出现了问题。我想显示数组的最后一条记录,更具体地说是重量。

抱歉,我收到一个空参数错误,尽管最后一个数组记录的print_r返回正确的数据。我究竟做错了什么?

$elevatorContent = array (

    array (
    "firstname" => "Peter",
    "weight" => 70
    ),

    array (
    "firstname" => "Cassie",
    "weight" => 70
    ),

    array (
    "firstname" => "John",
    "weight" => 150
    ),

    array (
    "firstname" => "Agnes",
    "weight" => 150
    ),

);  

返回null

echo "<p>The last person in elevator is <b>".end($elevatorContent['firstname']). "<b></p>";

4 个答案:

答案 0 :(得分:3)

您需要进行小幅修正:

php end()返回给定数组的最后一个元素,因此您必须先获取最后一个元素,然后通过您的密钥访问它。

自PHP 5.4起:

end($elevatorContent['firstname'])

echo end($elevatorContent)["firstname"];

在PHP 5.3或更早版本中,您需要使用临时变量。

$last_arr = end($elevatorContent);
echo $last_arr["firstname"];

所以它变成了:

echo "<p>The last person in elevator is <b>" . end($elevatorContent)['firstname'] . "<b></p>";

OR

$last_arr = end($elevatorContent);
echo "<p>The last person in elevator is <b>".$last_arr["firstname"]. "<b></p>";

答案 1 :(得分:1)

varying vec3 vWorldPosition;

void main() {
    //With transformDirection:
    //vWorldPosition = normalize( ( modelMatrix * vec4( position, 0.0 ) ).xyz );
    //With just the world position (interpreted by the textureCube as a direction)
    vWorldPosition = ( modelMatrix * vec4( position, 1.0 ) ).xyz;

    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}

答案 2 :(得分:0)

<强>尝试:

echo "<p>The last person in elevator is <b>" . $elevatorContent[count($elevatorContent) - 1]['firstname'] . "<b></p>";

答案 3 :(得分:0)

[HttpPost]
        public ActionResult Index(LoginDetails log)
        {
            if (log.UserId == "Admin")
            {
               SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString);

            }
            else if (log.UserId == "Customer")
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SqlConnection1"].ConnectionString);
            }
            else
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SqlConnection2"].ConnectionString);
            }
            return View();            
        }