我想访问最后一个键并评估它是怎么可能的?

时间:2019-07-09 07:02:46

标签: php codeigniter-2

数组 (     [状态] =>数组         (             [] =>选择状态             [1] =>安达曼和尼科巴(AN)             [2] =>安得拉邦(AP)             [3] =>阿鲁纳恰尔邦(AR)             [4] =>阿萨姆邦(AS)             [5] =>比哈尔(BR)             [6] =>昌迪加(CH)             [7] =>恰蒂斯加尔邦(CG)             [8] =>达德拉和纳加尔·哈维里(DN)             [9] =>达曼和丢(DD)             [10] =>德里(DL)             [11] =>果阿(GA)             [12] =>古吉拉特(GJ)             [13] =>哈里亚纳邦(HR)             [14] =>喜马al尔邦(HP)             [15] =>查mu和克什米尔(JK)             [16] =>贾坎德邦(JH)             [17] =>卡纳塔克邦(KA)             [18] =>喀拉拉邦(KL)             [19] => Lakshdweep(LD)             [20] =>中央邦(MP)             [21] =>马哈拉施特拉邦(MH)             [22] =>曼尼普尔(MN)             [23] =>梅加拉亚邦(ML)             [24] =>米佐拉姆(MZ)             [25] =>那加兰(NL)             [26] =>奥里萨(OD)             [27] => Puducherry(PY)             [28] =>旁遮普语(PB)             [29] =>拉贾斯坦(RJ)             [30] =>锡金(SK)             [31] =>泰米尔纳德邦(TN)             [32] => Telangana(TG)             [33] => Tripura(TR)             [34] =>北方邦(UP)             [35] =>北阿坎德邦(英国)             [36] =>西孟加拉邦(WB)         )

[captcha_image] => 
[browser_details] => Chrome
[browser_version] => 75.0.3770.100
[user_ip_address] => ::1
[0] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[1] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[2] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[3] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[4] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[5] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[6] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[7] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[8] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[9] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[10] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[11] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[12] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[13] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[14] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[15] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[16] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[17] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[18] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[19] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[20] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[21] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[22] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[23] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[24] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[25] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[26] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[27] => stdClass Object
    (
        [date_added] => 0000-00-00 00:00:00
    )

[28] => stdClass Object
    (
        [date_added] => 2019-07-08 15:14:38
    )

2 个答案:

答案 0 :(得分:1)

1- 我想访问最后一个密钥并评估可能的方式?

<?php
    //Append the array value in variable $array, After that follow the below step.
      end($array['states']); // The end() function moves the internal pointer to, and outputs, the last element in the array.

        $key = key($array['states']);  // get the last key
        echo $value = $array['states'][$key]; // get last value be key
        ?>

DEMO

2-基于注释的答案“我如何才能访问每个值,尤其是对象数组?”

//Append your object_array in variable call array
$array = object_array; 
//Using loop you can get the value of object. 
foreach ($array as $value) { 
echo $value->date_added ; 
} 

答案 1 :(得分:0)

尝试一下

 end($array['states']);
 $z = prev($array['states']);