简体   繁体   English

PHP-访问多维数组

[英]PHP - Access multidimensional array

I've read a lot of answers here on SO but I am still troubled when it comes to getting my head around how to access the correct part of my multidimensional array. 我在SO上已经阅读了很多答案,但是当谈到如何访问多维数组的正确部分时,我仍然感到困惑。

What I am after is each client_img > sizes > w720h720c1 > path . 我需要的是每个client_img>尺寸> w720h720c1> path

My array looks like: 我的数组看起来像:

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 
    ) 
) 

I've tried this: 我已经试过了:

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

But I get an error of: Undefined index: sizes 但是我得到一个错误: 未定义的索引:大小

Guidance on what I am doing wrong would be much appreciated. 关于我做错了什么的指导将不胜感激。

You don't want to loop through the items inside of client_img , you just want the sizes: 您不想遍历client_img内的项目,而只想要大小:

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

(you don't want to echo the value either, since it is an array) (您也不希望echo该值,因为它是一个数组)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM