将数据从php while循环添加到数组中

问题描述 投票:0回答:1

我有此代码,我想向$meta_input_array变量添加数据。当我添加单个数据时可以,但是当我要添加多个数据时不起作用

这是我的代码:

<?php

$spa_products = array(
  'post_type' => 'spa_products'
);

$spa = new WP_Query( $spa_products );

if ( $spa->have_posts() ) {

  $row = 0;

  while ( $spa->have_posts() ) { $spa->the_post();

    $product_POST = 'product_' . get_the_ID();

    $product_normal_price = get_post_meta( get_the_ID(), 'spa_normal_price', true );
    $product_gold_price = get_post_meta( get_the_ID(), 'spa_gold_price', true );

    if ( !empty( $_POST[$product_POST] ) ) {

      if ( $_POST[$product_POST] === $product_normal_price ) {
        $product_package = $product_normal_price;
        $product_package_show = 'بسته معمولی';
      } elseif ( $_POST[$product_POST] === $product_gold_price ) {
        $product_package = $product_gold_price;
        $product_package_show = 'بسته طلائی';
      }

      echo '<tr>';

      echo '<td>' . get_the_title() . ' " <small>' . $product_package_show . '<small> " </td><td>' . number_format( $product_package ) . ' ریال</td>';

      echo '</tr>';

      $invoice_total[] = $product_package;

      $meta_input_array_row = 'row_' . $row++;
      $meta_input_array[] = array( $meta_input_array_row => $product_package_show );

    }
  }
}

$spa_new_order = array(
  'post_type'     => 'spa_orders',
  'post_title'    => $_POST['product_number'],
  'post_status'   => 'draft',
  'meta_input'    => $meta_input_array,
);

wp_insert_post( $spa_new_order );

?>

@ ron这是结果:

array(2) { [0]=> array(1) { ["row_0"]=> string(19) "gold" } [1]=> array(1) { ["row_1"]=> string(21) "normal" } } 
arrays wordpress post while-loop
1个回答
0
投票

尝试更改:

      $meta_input_array[] = array( $meta_input_array_row => $product_package_show );

to

      $meta_input_array[$meta_input_array_row] = $product_package_show;
© www.soinside.com 2019 - 2024. All rights reserved.