admin管理员组

文章数量:1289845

I have written the code with many update_post_meta entries:

update_post_meta($order_id, '_shipping_first_name', $order->get_billing_first_name() );
update_post_meta($order_id, '_shipping_last_name', $order->get_billing_last_name() );
update_post_meta($order_id, '_shipping_company', $order->get_billing_company() );
update_post_meta($order_id, '_shipping_address_1', $order->get_billing_address_1() );
update_post_meta($order_id, '_shipping_address_2', $order->get_billing_address_2() );
update_post_meta($order_id, '_shipping_postcode', $order->get_billing_postcode() );
update_post_meta($order_id, '_shipping_city', $order->get_billing_city() );
update_post_meta($order_id, '_shipping_state', $order->get_billing_state() );
update_post_meta($order_id, '_shipping_country', $order->get_billing_country() );

For DRY principle I tried to use an array with for loops, but it doesn't work:

$data = array(
    array( '_shipping_first_name', $order->get_billing_first_name() ),
    array( '_shipping_last_name', $order->get_billing_last_name() ),
    array( '_shipping_company', $order->get_billing_company() ),
    array( '_shipping_address_1', $order->get_billing_address_1() ),
    array( '_shipping_address_2', $order->get_billing_address_2() ),
    array( '_shipping_postcode', $order->get_billing_postcode() ),
    array( '_shipping_city', $order->get_billing_city() ),
    array( '_shipping_state', $order->get_billing_state() ),
    array( '_shipping_country', $order->get_billing_country() )
);

for ( $row = 0; $row < 9; $row++) {
    for ( $col = 0; $col < 2; $col++) {
        update_post_meta( $order_id, [$row], [$col] );
    }
}

I have written the code with many update_post_meta entries:

update_post_meta($order_id, '_shipping_first_name', $order->get_billing_first_name() );
update_post_meta($order_id, '_shipping_last_name', $order->get_billing_last_name() );
update_post_meta($order_id, '_shipping_company', $order->get_billing_company() );
update_post_meta($order_id, '_shipping_address_1', $order->get_billing_address_1() );
update_post_meta($order_id, '_shipping_address_2', $order->get_billing_address_2() );
update_post_meta($order_id, '_shipping_postcode', $order->get_billing_postcode() );
update_post_meta($order_id, '_shipping_city', $order->get_billing_city() );
update_post_meta($order_id, '_shipping_state', $order->get_billing_state() );
update_post_meta($order_id, '_shipping_country', $order->get_billing_country() );

For DRY principle I tried to use an array with for loops, but it doesn't work:

$data = array(
    array( '_shipping_first_name', $order->get_billing_first_name() ),
    array( '_shipping_last_name', $order->get_billing_last_name() ),
    array( '_shipping_company', $order->get_billing_company() ),
    array( '_shipping_address_1', $order->get_billing_address_1() ),
    array( '_shipping_address_2', $order->get_billing_address_2() ),
    array( '_shipping_postcode', $order->get_billing_postcode() ),
    array( '_shipping_city', $order->get_billing_city() ),
    array( '_shipping_state', $order->get_billing_state() ),
    array( '_shipping_country', $order->get_billing_country() )
);

for ( $row = 0; $row < 9; $row++) {
    for ( $col = 0; $col < 2; $col++) {
        update_post_meta( $order_id, [$row], [$col] );
    }
}
Share Improve this question asked Jul 6, 2021 at 20:27 DomaruDomaru 31 bronze badge 1
  • 1 I'm not sure why you're using 2 for loops, wouldn't it be easier to use 'key' => $order->...? Then a foreach ( $data as $key => $value )? [$row] will just be a new array containing a number from the for loop. Arrays in PHP can have named indexes and act as dictionaries, numbered indices are unusual and rarely needed – Tom J Nowell Commented Jul 6, 2021 at 21:04
Add a comment  | 

1 Answer 1

Reset to default 1

To achieve what you want you're better creating an associative array and then walking over it with a foreach loop as shown below :

$data = array(
    '_shipping_first_name' => $order->get_billing_first_name(), 
    '_shipping_last_name' => $order->get_billing_last_name(), 
    '_shipping_company' => $order->get_billing_company(), 
    '_shipping_address_1' => $order->get_billing_address_1(), 
    '_shipping_address_2' => $order->get_billing_address_2(), 
    '_shipping_postcode' => $order->get_billing_postcode(), 
    '_shipping_city' => $order->get_billing_city(), 
    '_shipping_state' => $order->get_billing_state(), 
    '_shipping_country' => $order->get_billing_country()
);

foreach ( $data as $meta_key => $meta_value ) {
        update_post_meta( $order_id, $meta_key, $meta_value );
}

本文标签: phpHow to optimize updatepostmeta