0

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
            )
    )
)
  • 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 Mar 22 '16 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 Mar 22 '16 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 Mar 22 '16 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 Mar 22 '16 at 22:20
4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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