dongtan4046 2015-03-26 20:26
浏览 19
已采纳

带有一个元素但索引未知的PHP数组。 如何获得?

A function is returning arrays which look like

( [4] => SomeObject )

or

( [2] => SomeObject )

If I know the array will always have only one child how can I access it without knowing the index?

I'm sure PHP has an equivalent to Javascript's getOwnPropertynames, but I would think there must be a more direct way.

Thanks!

  • 写回答

1条回答 默认 最新

  • dpbf62565 2015-03-26 20:28
    关注

    You can use current:

    $value = current($arr);
    

    current returns the value of the element currently pointed to by the array's internal pointer. In this case, that will be the first (and only) element of the array.

    Testing it out:

    function f() {
        return array(4 => 'foo');
    }
    
    $arr = f();
    echo current($arr);
    

    Output:

    foo
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动