我在 PHP 中有一个数组。数组可以是 X 深。数组中的某处是一个带有 key 的对象custom_image
。我想用我自己的数据替换对象。有关结构的示例,请参见随附的屏幕截图。请注意,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] =>
)
)
)