1

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
            )
    )
)
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. 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. 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 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 Mar 22, 2016 at 22:20

1 Answer 1

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.com',
                'user_defined_field2' => 'http://www.domain-002.com',
            ),

            array(
                'user_defined_field1' => 'http://www.domain-011.com',
                'user_defined_field2' => 'http://www.domain-012.com',
            ),
        ),

        'repeater-2' => array(
            array(
                'user_defined_field1' => 'http://www.domain-101.com',
                'user_defined_field2' => 'http://www.domain-102.com',
            ),

            array(
                'user_defined_field1' => 'http://www.domain-111.com',
                'user_defined_field2' => 'http://www.domain-112.com',
            ),
        ),
    );

   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 ); 
1
  • That's what i am talking about, You're my hero, Working like a charm.. Mar 22, 2016 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.