duansaoguan7955 2012-06-19 21:31
浏览 48
已采纳

在Drupal中访问多维数组

This is as equally a drupal question as a PHP question, I assume.

I have the following print_r($node) array

stdClass Object
(
    [vid] => 4
...
    [field_imgleft] => Array
            (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => defaultimgleft
                            [format] => 
                            [safe_value] => defaultimgleft
                        )
                )
        )
)

field_imgleft is a field in the content type of the node. Since there will only be one value, [0] is the max for that array. I'm trying to return the value of [value] to a variable, but I am having no such luck with node-> methods or node[...] and so on.

  • 写回答

2条回答 默认 最新

  • douxi2011 2012-06-19 22:00
    关注

    There's a built in API function to extract field values from an entity: field_get_items().

    You can use it like so:

    $items = field_get_items('node', $node, 'field_imgleft');
    
    $first_item = array_shift($items);
    
    $value = $first_item['value'];
    

    This method is recommended over accessing the array directly as it takes care of the field translation for you...no need to worry about using 'und' any more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改