【问题标题】:add data to array from php while loop将数据从php while循环添加到数组
【发布时间】:2020-08-24 15:31:52
【问题描述】:

我有这段代码,我想将数据添加到$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" } } 

【问题讨论】:

  • 您的具体错误是什么?
  • @Ron 我没有收到任何错误,但我的数据没有导入
  • @Ron 我的问题是$meta_input_array[]
  • 您的“数据未导入”可能有很多原因。在$spa_new_order = 行之前,您可以输入var_dump($meta_input_array); 并重新运行您的脚本,将会生成输出,您可以将该输出添加到上面的问题中吗?
  • @Ron 我加了。

标签: arrays wordpress post while-loop


【解决方案1】:

尝试改变:

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

      $meta_input_array[$meta_input_array_row] = $product_package_show;

【讨论】:

    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 2012-04-15
    • 2013-07-29
    • 2016-08-25
    • 2020-05-13
    • 2022-08-09
    • 2012-02-21
    • 1970-01-01
    相关资源
    最近更新 更多