代码之家  ›  专栏  ›  技术社区  ›  Drew Baker

php替换未知深度的多维数组中的元素

  •  0
  • Drew Baker  · 技术社区  · 7 年前

    我有一个PHP数组。数组可以是x深。数组中的某个地方有一个带键的对象 custom_image . 我想用我自己的数据替换对象。结构示例见附截图。注意 顾客形象 通常是在数组中的不同位置。

    我试着用 array_walk_recursive() 但它不能处理数组中的键,所以我尝试滚动自己的递归 array_walk() 递归 foreach() 功能,但无法使其工作。

    据我所知,这应该有效:

        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');
           }
        }
    

    但实际上它似乎将父数组替换为它所在的数组,所以很明显我对它的工作方式感到困惑。

    谢谢你的帮助!

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

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rasclatt    7 年前

    一个简单的递归函数应该做到:

    /**
     *  @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;
    }
    

    使用:

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