admin管理员组文章数量:1022719
I've a huge multidimensional array with no-way to foreach it :), Anyway, i want to replace all http
to a keyword so if the client convert his website into https
he won't change all old images for the new url.
I tried array_map
, array_walk
and of course str_replace
but i found that i must make infinite foreach for every nested array.
Array (
[Repeater_1] => Array (
[1] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
[2] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
)
[Repeater_2] => Array (
[1] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
[2] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
)
)
I've a huge multidimensional array with no-way to foreach it :), Anyway, i want to replace all http
to a keyword so if the client convert his website into https
he won't change all old images for the new url.
I tried array_map
, array_walk
and of course str_replace
but i found that i must make infinite foreach for every nested array.
Array (
[Repeater_1] => Array (
[1] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
[2] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
)
[Repeater_2] => Array (
[1] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
[2] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
)
)
Share
Improve this question
edited Mar 23, 2016 at 7:32
Pieter Goosen
55.4k23 gold badges116 silver badges210 bronze badges
asked Mar 22, 2016 at 22:06
Hady ShaltoutHady Shaltout
1521 silver badge9 bronze badges
4
- if you don't want a loop how would you use it then? I don't think you can just use the data without loop. – nonsensecreativity Commented Mar 22, 2016 at 22:13
- I just ask for an easy way like array_map or array_walk to perform a function to all array items. – Hady Shaltout Commented Mar 22, 2016 at 22:15
- array_map or array_walk still using loop, it just you write less code about it, and the loop is performed by php itself, otherwise there is no way to apply callback function to each of the array value – nonsensecreativity Commented Mar 22, 2016 at 22:18
- is there no way to change the array value for the URL without the protocol? what did you use to generate the data? if you can change the raw data itself, you may use esc_url() later for displaying the url – nonsensecreativity Commented Mar 22, 2016 at 22:20
1 Answer
Reset to default 6Try this php built-in function array_walk_recursive
function wpse_do_something_on_data() {
$data = array(
'repeater-1' => array(
array(
'user_defined_field1' => 'http://www.domain-001',
'user_defined_field2' => 'http://www.domain-002',
),
array(
'user_defined_field1' => 'http://www.domain-011',
'user_defined_field2' => 'http://www.domain-012',
),
),
'repeater-2' => array(
array(
'user_defined_field1' => 'http://www.domain-101',
'user_defined_field2' => 'http://www.domain-102',
),
array(
'user_defined_field1' => 'http://www.domain-111',
'user_defined_field2' => 'http://www.domain-112',
),
),
);
array_walk_recursive( $data, 'wpse_callback' );
return $data;
}
function wpse_callback( &$value, $key ) {
$value = str_replace( 'http://', 'keyword', $value );
}
$my_data = wpse_do_something_on_data();
var_dump( $my_data );
I've a huge multidimensional array with no-way to foreach it :), Anyway, i want to replace all http
to a keyword so if the client convert his website into https
he won't change all old images for the new url.
I tried array_map
, array_walk
and of course str_replace
but i found that i must make infinite foreach for every nested array.
Array (
[Repeater_1] => Array (
[1] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
[2] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
)
[Repeater_2] => Array (
[1] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
[2] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
)
)
I've a huge multidimensional array with no-way to foreach it :), Anyway, i want to replace all http
to a keyword so if the client convert his website into https
he won't change all old images for the new url.
I tried array_map
, array_walk
and of course str_replace
but i found that i must make infinite foreach for every nested array.
Array (
[Repeater_1] => Array (
[1] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
[2] => Array (
[user_defined_field_1] => http://Value_1
[user_defined_field_2] => http://Value_2
)
)
[Repeater_2] => Array (
[1] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
[2] => Array (
[user_defined_field_3] => http://Value_1
[user_defined_field_4] => http://Value_2
)
)
)
Share
Improve this question
edited Mar 23, 2016 at 7:32
Pieter Goosen
55.4k23 gold badges116 silver badges210 bronze badges
asked Mar 22, 2016 at 22:06
Hady ShaltoutHady Shaltout
1521 silver badge9 bronze badges
4
- if you don't want a loop how would you use it then? I don't think you can just use the data without loop. – nonsensecreativity Commented Mar 22, 2016 at 22:13
- I just ask for an easy way like array_map or array_walk to perform a function to all array items. – Hady Shaltout Commented Mar 22, 2016 at 22:15
- array_map or array_walk still using loop, it just you write less code about it, and the loop is performed by php itself, otherwise there is no way to apply callback function to each of the array value – nonsensecreativity Commented Mar 22, 2016 at 22:18
- is there no way to change the array value for the URL without the protocol? what did you use to generate the data? if you can change the raw data itself, you may use esc_url() later for displaying the url – nonsensecreativity Commented Mar 22, 2016 at 22:20
1 Answer
Reset to default 6Try this php built-in function array_walk_recursive
function wpse_do_something_on_data() {
$data = array(
'repeater-1' => array(
array(
'user_defined_field1' => 'http://www.domain-001',
'user_defined_field2' => 'http://www.domain-002',
),
array(
'user_defined_field1' => 'http://www.domain-011',
'user_defined_field2' => 'http://www.domain-012',
),
),
'repeater-2' => array(
array(
'user_defined_field1' => 'http://www.domain-101',
'user_defined_field2' => 'http://www.domain-102',
),
array(
'user_defined_field1' => 'http://www.domain-111',
'user_defined_field2' => 'http://www.domain-112',
),
),
);
array_walk_recursive( $data, 'wpse_callback' );
return $data;
}
function wpse_callback( &$value, $key ) {
$value = str_replace( 'http://', 'keyword', $value );
}
$my_data = wpse_do_something_on_data();
var_dump( $my_data );
本文标签: customizationReplace text inside a huge multidimensional array
版权声明:本文标题:customization - Replace text inside a huge multidimensional array 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745573807a2156897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论