PHP - 访问多维数组

时间:2017-05-10 22:27:47

标签: php arrays multidimensional-array

我已经在这里阅读了很多答案,但是当谈到如何访问我的多维数组的正确部分时,我仍然感到困扰。

我所追求的是每个 client_img>尺寸> w720h720c1>路径

我的数组看起来像:

Array ( 
[0] => Array ( 
    [_id] => 26 
    [client_text] => This is text from mathews testimonial 
    [client_img] => Array ( 
        [assetID] => 9 
        [title] => Client img 
        [_default] => /perch/resources/clientimg.jpg 
        [bucket] => default 
        [path] => clientimg.jpg 
        [size] => 421055 [w] => 736 [h] => 443 
        [mime] => image/jpeg 
        [sizes] => Array ( 
            [thumb] => Array ( 
                [w] => 150 [h] => 90 
                [target_w] => 150 
                [target_h] => 150 
                [density] => 2 
                [path] => clientimg-thumb@2x.jpg 
                [size] => 25217 
                [mime] => image/jpeg 
                [assetID] => 10 
            ) 
            [w720h720c1] => Array ( 
                [w] => 720 
                [h] => 433 
                [target_w] => 720 
                [target_h] => 720 
                [crop] => true 
                [density] => 1 
                [path] => clientimg-w720h720.jpg 
                [size] => 88584 
                [mime] => 
                [assetID] => 91 
                ) 
            )
        ) 
        [client_name] => 
        Mathew D 
        [_page] => * 
        [_pageID] => 1 
        [_sortvalue] => Mathew D 
    ) 
    [1] => Array ( 
        [_id] => 24 
        [client_text] => This is text from paragraph one 
        [client_img] => Array ( 
            [assetID] => 3 
            [title] => Service 1 
            [_default] => /perch/resources/service1.jpg 
            [bucket] => default 
            [path] => service1.jpg 
            [size] => 449233 
            [w] => 800 
            [h] => 450 
            [mime] => image/jpeg 
            [sizes] => Array ( 
                [thumb] => Array ( 
                    [w] => 150 
                    [h] => 84 
                    [target_w] => 150 
                    [target_h] => 150 
                    [density] => 2 
                    [path] => service1-thumb@2x.jpg 
                    [size] => 22588 
                    [mime] => image/jpeg 
                    [assetID] => 4 
                ) 
                [w720h720c1] => Array ( 
                    [w] => 720 
                    [h] => 405 
                    [target_w] => 720 
                    [target_h] => 720 
                    [crop] => true 
                    [density] => 1 
                    [path] => service1-w720h720.jpg 
                    [size] => 86733 
                    [mime] => 
                    [assetID] => 90 
                ) 
            )
        ) 
        [client_name] => Rosie walker 
        [_page] => * 
        [_pageID] => 1 
        [_sortvalue] => Rosie walker 
    ) 
) 

我试过这个:

foreach($testimonials as $testimonial) {
    foreach($testimonial['client_img'] as $image) {
        echo $image['sizes']['w720h720c1'];
    }
}

但我收到的错误是:未定义索引:尺寸

我非常感谢您对我所做错事的指导。

1 个答案:

答案 0 :(得分:0)

您不想遍历client_img内的项目,只需要大小:

foreach($testimonials as $testimonial) {
    print_r($testimonial['client_img']['sizes']['w720h720c1']);
}

(你不想echo这个值,因为它是一个数组)