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 Answer
Reset to default 1To 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
版权声明:本文标题:php - How to optimize update_post_meta? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741460614a2380017.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'key' => $order->...
? Then aforeach ( $data as $key => $value )
?[$row]
will just be a new array containing a number from thefor
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