admin管理员组

文章数量:1431943

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
Add a comment  | 

1 Answer 1

Reset to default 6

Try 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