dongliang9682 2015-10-28 02:49
浏览 11
已采纳

无法在PHP中从数组中获取数据

I've got this array:

array(1) { 
   ["sensory-evaluation"]=> array(6) { 
     ["name"]=> string(18) "Sensory    Evaluation" 
     ["value"]=> string(43) "10/22/2015 at 6:00pm | 11/25/2015 at 6:00pm"
     ["position"]=> string(1) "3" 
     ["is_visible"]=> int(1) 
     ["is_variation"]=> int(1) 
     ["is_taxonomy"]=> int(0) 
    } 
} 

I need to be able to get the data from ["value"]. If I do $arr["sensory-evaluation"]["value"] I can get it but the problem is ["sensory-evaluation"] will be different for each element in my array so I need a way to abstract that part but I haven't been able to figure it out.

  • 写回答

2条回答 默认 最新

  • dongzheng7165 2015-10-28 03:12
    关注

    If you have only one item in the array as you show then:

    echo current($arr)['value'];
    

    If you don't know if it exists:

    if(isset(current($arr)['value'])) {
        echo current($arr)['value'];
    }
    

    You could also do:

    echo array_values($arr)[0]['value'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP