【问题标题】:Trying to access data within php array试图访问php数组中的数据
【发布时间】:2014-06-19 03:15:08
【问题描述】:

如果我将此数组作为 json 返回:

$events['data']

我明白了:

[
    {
        "id":"3"
    }
]

但如果我尝试返回 id,我会得到 Undefined index: id,使用以下代码:

$events['data']['id']

如何访问这些数据?

编辑:在下面添加了 $events['data'] 的 var_dump:

array(1) {
  [0]=>
  array(6) {
    ["id"]=>
    string(15) "299452650179381"
    ["name"]=>
    string(23) "Body of Work Exhibition"
    ["description"]=>
    string(380) "Friday 23rd May - Private view from 6-9pm. Part of Dartmouth Galleries Week.
A group show of 11 artists including Clare Cutts, Aliisa Hyslop, Janet Mitchell, Lynn Muir wooden figures, Jane Ryan OPI Kathryn O'Kell, Sarah Saunders Ceramics, Kate Glanville - Plates and Tiles, Karen James,  EllyMental Jewellery and Elvis the Kat
All work will feature bodies whether human or animal."
    ["cover"]=>
    array(4) {
      ["cover_id"]=>
      string(15) "750312108326721"
      ["source"]=>
      string(101) "https://scontent-b.xx.fbcdn.net/hphotos-prn2/t1.0-9/s720x720/1947331_750312108326721_1639418640_n.jpg"
      ["offset_y"]=>
      int(30)
      ["offset_x"]=>
      int(0)
    }
    ["start_time"]=>
    string(24) "2014-05-23T18:00:00+0100"
    ["end_time"]=>
    string(24) "2014-06-12T17:00:00+0100"
  }
}

【问题讨论】:

  • 这里缺少 id 的右引号 ['id]
  • 尝试var_dump($events['data']) 获取数组的 PHP 视图,这应该可以更明显地看出那里还有一个数组级别
  • 你必须json_encode($events) 才能做到这一点,而不是json_encode($events['data'])
  • @Jenz,抱歉打错了。
  • $events['data'][0]['id']

标签: php json laravel


【解决方案1】:
foreach($events as $v){
$id = $v['id'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多