从数组中获取特定值

时间:2012-04-13 11:14:10

标签: php arrays

我有这个数组:

    stdClass Object
(
    [nid] => 218
    [node_title] => This is my title text
    [field_data_field_slider_image_delta] => 0
    [field_data_field_slider_image_language] => und
    [field_data_field_slider_image_bundle] => slider
    [field_data_field_slider_image_field_slider_image_fid] => 1278
    [field_data_field_slider_image_field_slider_image_alt] => 
    [field_data_field_slider_image_field_slider_image_title] => 
    [field_data_field_slider_image_field_slider_image_width] => 1180
    [field_data_field_slider_image_field_slider_image_height] => 352
    [node_created] => 1334267878
    [field_data_field_slider_link_node_entity_type] => node
    [field_data_body_node_entity_type] => node
    [field_data_field_slider_image_node_entity_type] => node
    [_field_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (
                            [vid] => 218
                            [uid] => 1
                            [title] => This is my title text
                            [log] => 
                            [status] => 1
                            [comment] => 1
                            [promote] => 1
                            [sticky] => 0
                            [nid] => 218
                            [type] => slider
                            [language] => en
                            [created] => 1334267878
                            [changed] => 1334268388
                            [tnid] => 0
                            [translate] => 0
                            [revision_timestamp] => 1334268388
                            [revision_uid] => 1
                            [body] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] =&gt; <p>This is my body text</p>
                                                    [summary] =&gt; 
                                                    [format] =&gt; full_html
                                                    [safe_value] =&gt;  <p>This is my body text</p> 
                                                    [safe_summary] =&gt;   
                                                )

                                        )

                                )

                            [field_slider_image] =&gt; Array
                                (
                                    [und] =&gt; Array
                                        (
                                            [0] =&gt; Array
                                                (
                                                    [fid] =&gt; 1278
                                                    [alt] =&gt; 
                                                    [title] =&gt; 
                                                    [width] =&gt; 1180
                                                    [height] =&gt; 352
                                                    [uid] =&gt; 1
                                                    [filename] =&gt; slider-d.jpg
                                                    [uri] =&gt; public://images/slider-d.jpg
                                                    [filemime] =&gt; image/jpeg
                                                    [filesize] =&gt; 103135
                                                    [status] =&gt; 1
                                                    [timestamp] =&gt; 1334267878
                                                    [type] =&gt; image
                                                )

                                        )

                                )

                            [field_slider_link] =&gt; Array
                                (
                                    [und] =&gt; Array
                                        (
                                            [0] =&gt; Array
                                                (
                                                    [url] =&gt; http://mydomain.com/my/link
                                                    [title] =&gt; 
                                                    [attributes] =&gt; Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                            [cid] =&gt; 0
                            [last_comment_timestamp] =&gt; 1334267878
                            [last_comment_name] =&gt; 
                            [last_comment_uid] =&gt; 1
                            [comment_count] =&gt; 0
                            [name] =&gt; admin
                            [picture] =&gt; 1242
                            [data] =&gt; a:2:{s:7:"contact";i:0;s:7:"overlay";i:1;}
                        )

                )

        )

    [field_field_slider_link] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [rendered] =&gt; Array
                        (
                            [#markup] =&gt; http://mydomain.com/my/link
                            [#access] =&gt; 1
                        )

                    [raw] =&gt; Array
                        (
                            [url] =&gt; http://mydomain.com/my/link
                            [title] =&gt; http://mydomain.com/my/link
                            [attributes] =&gt; Array
                                (
                                )

                            [display_url] =&gt; http://mydomain.com/my/link
                        )

                )

        )

    [field_body] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [rendered] =&gt; Array
                        (
                            [#markup] =&gt;  <p>This is my body text</p> 
                            [#access] =&gt; 1
                            [#type] =&gt; markup
                            [#pre_render] =&gt; Array
                                (
                                    [0] =&gt; drupal_pre_render_markup
                                    [1] =&gt; ctools_dependent_pre_render
                                )

                            [#children] =&gt;  <p>This is my body text</p> 
                            [#printed] =&gt; 1
                        )

                    [raw] =&gt; Array
                        (
                            [value] =&gt; <p>This is my body text</p>
                            [summary] =&gt; 
                            [format] =&gt; full_html
                            [safe_value] =&gt;  <p>This is my body text</p> 
                            [safe_summary] =&gt;   
                            [#children] =&gt; 
                            [#printed] =&gt; 1
                        )

                    [#children] =&gt;  <p>This is my body text</p> 
                    [#printed] =&gt; 1
                )

            [#children] =&gt;  <p>This is my body text</p> 
            [#printed] =&gt; 1
        )

    [field_field_slider_image] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [rendered] =&gt; Array
                        (
                            [#theme] =&gt; image_formatter
                            [#item] =&gt; Array
                                (
                                    [fid] =&gt; 1278
                                    [alt] =&gt; 
                                    [title] =&gt; 
                                    [width] =&gt; 1180
                                    [height] =&gt; 352
                                    [uid] =&gt; 1
                                    [filename] =&gt; slider-d.jpg
                                    [uri] =&gt; public://images/slider-d.jpg
                                    [filemime] =&gt; image/jpeg
                                    [filesize] =&gt; 103135
                                    [status] =&gt; 1
                                    [timestamp] =&gt; 1334267878
                                    [type] =&gt; image
                                )

                            [#image_style] =&gt; featured_slideshow
                            [#path] =&gt; 
                            [#access] =&gt; 1
                        )

                    [raw] =&gt; Array
                        (
                            [fid] =&gt; 1278
                            [alt] =&gt; 
                            [title] =&gt; 
                            [width] =&gt; 1180
                            [height] =&gt; 352
                            [uid] =&gt; 1
                            [filename] =&gt; slider-d.jpg
                            [uri] =&gt; public://images/slider-d.jpg
                            [filemime] =&gt; image/jpeg
                            [filesize] =&gt; 103135
                            [status] =&gt; 1
                            [timestamp] =&gt; 1334267878
                            [type] =&gt; image
                        )

                )

        )

)

我需要获取[url]值(在此http://mydomain.com/my/link}

我尝试按照教程进行操作,结果如下:

print $row->field_field_slider_link[0]['raw']['url']

但我收到此错误消息:

Notice: Undefined offset: 0 in include() (line 57 of /var/www/vhosts/clientname/public_html/sites/all/themes/custom/templates/views/ff-nivo-slider/views-view-fields--ff-nivo-slider.tpl.php).

谁能看到我在这里做错了什么?

由于 ç

1 个答案:

答案 0 :(得分:2)

您的代码运行的数组不是您期望的数组。检查$row->field_field_slider_link的值。可能是它是一个空数组。