简体   繁体   中英

PHP replace an element in a multidimensional array of unknown depth

I have an array in PHP. The array could be X deep. Somewhere in the array is an object with key custom_image . I want to replace the object with my own data. See attached screenshot for example of structure. Note the custom_image is often times at a different place in the array.

I tried using array_walk_recursive() but it can't deal with keys that are arrays, so I tried rolling my own recursive array_walk() and recursive foreach() functions, but couldn't get it to work.

As far as I understand it, this should work:

    array_walk($array, 'replace_custom_image');

    function replace_custom_image(&$value, $key){
       if( $key == 'custom_image' ){
           $value = 'New image data'
       } else {
           // Make recursive so we can work with key's that contain an array!
           if( is_array($value) ) array_walk ($value, 'replace_custom_image');
       }
    }

But it actually seems to replace a parent array to the one it's on, so clearly I'm confused to the way it's working.

Thanks for the help!

Array
(
    [custom_credits] => Words hosted by: Name Here
Photos hosted by: Name Here
    [custom_button_text] => 
    [custom_link_url] => 
    [custom_sections] => Array
        (
            [0] => Array
                (
                    [custom_template_type] => slide-show
                    [custom_slide_meta] => Array
                        (
                            [0] => Array
                                (
                                    [custom_title] => Some title
                                    [custom_excerpt] => ewh e5gwer gt . w4tghwers 4twhrestghtrn qegrw w
                                    [custom_credits] => Words by: Name Here
                                    [custom_button_text] => Watch
                                    [custom_button_url] => http://google.com
                                    [custom_image] => Array
                                        (
                                            [ID] => 94
                                            [id] => 94
                                            [title] => superman-759
                                            [filename] => superman-759.jpg
                                            [filesize] => 49237
                                            [url] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                            [link] => http://prohbtddev.local/home/superman-759/
                                            [alt] => 
                                            [author] => 1
                                            [description] => 
                                            [caption] => 
                                            [name] => superman-759
                                            [status] => inherit
                                            [uploaded_to] => 10
                                            [date] => 2019-01-08 17:59:20
                                            [modified] => 2019-01-11 00:57:49
                                            [menu_order] => 0
                                            [mime_type] => image/jpeg
                                            [type] => image
                                            [subtype] => jpeg
                                            [icon] => http://prohbtddev.local/wp-includes/images/media/default.png
                                            [width] => 759
                                            [height] => 422
                                            [sizes] => Array
                                                (
                                                    [thumbnail] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759-150x150.jpg
                                                    [thumbnail-width] => 150
                                                    [thumbnail-height] => 150
                                                    [medium] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759-300x167.jpg
                                                    [medium-width] => 300
                                                    [medium-height] => 167
                                                    [medium_large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [medium_large-width] => 759
                                                    [medium_large-height] => 422
                                                    [large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [large-width] => 759
                                                    [large-height] => 422
                                                    [post-thumbnail] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [post-thumbnail-width] => 600
                                                    [post-thumbnail-height] => 334
                                                    [read-thumbnail-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [read-thumbnail-small-width] => 300
                                                    [read-thumbnail-small-height] => 167
                                                    [read-thumbnail-large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [read-thumbnail-large-width] => 759
                                                    [read-thumbnail-large-height] => 422
                                                    [watch-preview-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [watch-preview-small-width] => 320
                                                    [watch-preview-small-height] => 178
                                                    [watch-preview] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [watch-preview-width] => 759
                                                    [watch-preview-height] => 422
                                                    [product-thumbnail-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-thumbnail-small-width] => 100
                                                    [product-thumbnail-small-height] => 56
                                                    [product-thumbnail] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-thumbnail-width] => 250
                                                    [product-thumbnail-height] => 139
                                                    [product-thumbnial-large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-thumbnial-large-width] => 500
                                                    [product-thumbnial-large-height] => 278
                                                    [product-image] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-image-width] => 759
                                                    [product-image-height] => 422
                                                    [product-image-large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-image-large-width] => 759
                                                    [product-image-large-height] => 422
                                                    [product-image-xlarge] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-image-xlarge-width] => 759
                                                    [product-image-xlarge-height] => 422
                                                    [poster-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [poster-small-width] => 350
                                                    [poster-small-height] => 195
                                                    [poster] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [poster-width] => 700
                                                    [poster-height] => 389
                                                    [social-preview] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759-600x315.jpg
                                                    [social-preview-width] => 600
                                                    [social-preview-height] => 315
                                                    [fullscreen-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [fullscreen-small-width] => 759
                                                    [fullscreen-small-height] => 422
                                                    [fullscreen] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [fullscreen-width] => 759
                                                    [fullscreen-height] => 422
                                                )

                                            [focus] => Array
                                                (
                                                    [x] => 50
                                                    [y] => 50
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [custom_title] => Title 2
                                    [custom_excerpt] => tgwer 234wg5w4e 4hrtyf ewrvds rethd sert
                                    [custom_credits] => Photos by: Name Here
                                    [custom_button_text] => Watch
                                    [custom_button_url] => http://google.com
                                    [custom_image] => Array
                                        (
                                            [ID] => 94
                                            [id] => 94
                                            [title] => superman-759
                                            [filename] => superman-759.jpg
                                            [filesize] => 49237
                                            [url] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                            [link] => http://prohbtddev.local/home/superman-759/
                                            [alt] => 
                                            [author] => 1
                                            [description] => 
                                            [caption] => 
                                            [name] => superman-759
                                            [status] => inherit
                                            [uploaded_to] => 10
                                            [date] => 2019-01-08 17:59:20
                                            [modified] => 2019-01-11 00:57:49
                                            [menu_order] => 0
                                            [mime_type] => image/jpeg
                                            [type] => image
                                            [subtype] => jpeg
                                            [icon] => http://prohbtddev.local/wp-includes/images/media/default.png
                                            [width] => 759
                                            [height] => 422
                                            [sizes] => Array
                                                (
                                                    [thumbnail] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759-150x150.jpg
                                                    [thumbnail-width] => 150
                                                    [thumbnail-height] => 150
                                                    [medium] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759-300x167.jpg
                                                    [medium-width] => 300
                                                    [medium-height] => 167
                                                    [medium_large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [medium_large-width] => 759
                                                    [medium_large-height] => 422
                                                    [large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [large-width] => 759
                                                    [large-height] => 422
                                                    [post-thumbnail] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [post-thumbnail-width] => 600
                                                    [post-thumbnail-height] => 334
                                                    [read-thumbnail-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [read-thumbnail-small-width] => 300
                                                    [read-thumbnail-small-height] => 167
                                                    [read-thumbnail-large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [read-thumbnail-large-width] => 759
                                                    [read-thumbnail-large-height] => 422
                                                    [watch-preview-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [watch-preview-small-width] => 320
                                                    [watch-preview-small-height] => 178
                                                    [watch-preview] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [watch-preview-width] => 759
                                                    [watch-preview-height] => 422
                                                    [product-thumbnail-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-thumbnail-small-width] => 100
                                                    [product-thumbnail-small-height] => 56
                                                    [product-thumbnail] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-thumbnail-width] => 250
                                                    [product-thumbnail-height] => 139
                                                    [product-thumbnial-large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-thumbnial-large-width] => 500
                                                    [product-thumbnial-large-height] => 278
                                                    [product-image] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-image-width] => 759
                                                    [product-image-height] => 422
                                                    [product-image-large] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-image-large-width] => 759
                                                    [product-image-large-height] => 422
                                                    [product-image-xlarge] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [product-image-xlarge-width] => 759
                                                    [product-image-xlarge-height] => 422
                                                    [poster-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [poster-small-width] => 350
                                                    [poster-small-height] => 195
                                                    [poster] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [poster-width] => 700
                                                    [poster-height] => 389
                                                    [social-preview] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759-600x315.jpg
                                                    [social-preview-width] => 600
                                                    [social-preview-height] => 315
                                                    [fullscreen-small] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [fullscreen-small-width] => 759
                                                    [fullscreen-small-height] => 422
                                                    [fullscreen] => http://prohbtddev.local/wp-content/uploads/2019/01/superman-759.jpg
                                                    [fullscreen-width] => 759
                                                    [fullscreen-height] => 422
                                                )

                                            [focus] => Array
                                                (
                                                    [x] => 50
                                                    [y] => 50
                                                )

                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [custom_template_type] => new-episodes
                    [custom_slide_meta] => 
                )

            [2] => Array
                (
                    [custom_template_type] => ssn-slide-show
                    [custom_slide_meta] => 
                )

        )

)

在此处输入图片说明

A simple recursive function should do:

/**
 *  @param  $array [mix] The original array
 *  @param  $skey [string] The key name you want to replace
 *  @param  $replace [mix] The new value you want assign
 */
function recurse($array, $skey, $replace)
{
    # I would convert objects to arrays, it's just easier to not have to
    # deal with a mix (in my opinion)
    if(is_object($array))
        $array  =   (array) $array;
    # Just send value back if not an array
    if(!is_array($array))
        return $array;
    # Loop array, try to match. No match, recurse
    foreach($array as $key => $value)
        $array[$key]    =   ($skey === $key)? $replace : recurse($value, $skey, $replace);
    # Return each array
    return $array;
}

To use:

$original_array = recurse($original_array, 'custom_image', $repacement_value);

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