我在PHP中有一个数组。该数组可能是X深的。数组中某个位置的对象是键为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] =>
)
)
)
答案 0 :(得分:0)
一个简单的递归函数应该做到:
/**
* @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);