简体   繁体   中英

How can I get a specific value in this PHP array?

I'm setting up a Cloudinary listing of uploaded videos and I want to get the metadata of that video.

I have a print_r($result) :

<pre>
Cloudinary\Api\Response Object
(
    [rate_limit_reset_at] => 123123123
    [rate_limit_allowed] => 123
    [rate_limit_remaining] => 123
    [storage:ArrayObject:private] => Array
        (
            [public_id] => dog
            [format] => mp4
            [version] => 123123123
            [resource_type] => video
            [type] => upload
            [created_at] => 2019-07-29T07:32:50Z
            [bytes] => 123123
            [width] => 321
            [height] => 456
            [backup] => 1
            [access_mode] => public
            [url] => http://res.cloudinary.com/demo/video/upload/dog.mp4
            [secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
            [next_cursor] => 123123123
            [derived] => Array
                (
                    [0] => Array
                        (
                            [transformation] => /jpg
                            [format] => jpg
                            [bytes] => 86438
                            [id] => 123123123
                            [url] => http://res.cloudinary.com//demo/video/upload/dog.jpg
                            [secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
                            [extension] => jpg
                        )

                    [1] => Array
                        (
                            [transformation] => t_media_lib_thumb/jpg
                            [format] => jpg
                            [bytes] => 3293
                            [id] => 12123123
                            [url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
                            [secure_url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
                            [extension] => jpg
                        )

                )

        )

)
</pre>

I tried getting the value using this format but it says undefined index. echo $result[4]['width'] or echo $result['storage:ArrayObject:private']['width'];

I talked to a cloudinary support ad he said that "you should be able to access the data returned within [storage:ArrayObject:private] by simply ignoring it and requesting the index that you desire directly from $result. Meaning you can use or and so on with the other paramters."

So basically, just removing the index solved my problem like so:

<?php echo $result['height']; ?> <?php echo $result['width']; ?>

Have a look at this https://github.com/cloudinary/cloudinary_php/blob/master/src/Api/Response.php in order to better understand the object you are getting back.

Afterwards, look at the ArrayObject documentation here https://www.php.net/manual/en/class.arrayobject.php , as your Response object extends this class. Using getArrayCopy() to transform your object into an array should be what you need.

Bear in mind that I am not familiar with the API and that there may be better options to get what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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