从php foreach循环生成CSV文件并创建多个数组

时间:2017-10-18 13:43:22

标签: php arrays csv

我试图从php生成一个csv文件,其中每一行都将显示产品信息。

到目前为止,我使用以下内容并且它正常工作,但我似乎无法将产品放在不同的行中,因为它们需要位于不同的数组中。

我首先创建了一个循环浏览器和项目的函数,并将每个产品字段添加到一个名为$ compProduct的空数组中。然后将它们存储在另一个数组中并返回。

function cart_items_array() { 

$carts2 = MultiCart\get_carts();

// All the products
$allTheCarts = array();

foreach ( $carts2 as $cart2_id => $cart2 ) {
     // get array of items contained in a cart ...
    $items2 = MultiCart\get_cart( $cart_id2 );

    foreach ( $items2 as $item2_id => $item2 ) {
            $compProduct = array();
            $product_name = get_post($item2['product_id'])->post_title; 
            $familyterms = wp_get_post_terms( $item2['product_id'], 'pa_product-family'); 
            $cat_terms = wp_get_post_terms( $item2['product_id'], 'pa_product-category'); 
            $product_sku = get_post_meta( $item2['product_id'], '_sku', true );

            $compProduct[] = $product_sku; 
            foreach ($cat_terms as $cat_term) { $compProduct[] = $cat_term->name; };
            foreach ($familyterms as $family) { $compProduct[] = $family->name; }; 
            $compProduct[] = $product_name;
            $compProduct[] = $item2['quantity'];
            $compProduct[] = $cart2['name'];

            // Store the complete product info
            $allTheCarts[] = $compProduct;
    }
} 


return $allTheCarts;

}

然后我尝试将此函数添加到$数组但它不能正常工作......它会继续在csv的每一行上返回数组。

// Create csv
function create_csv_string($data) {

  // Open temp file pointer
  if (!$fp = fopen('php://temp', 'w+')) return FALSE;

  // Loop data and write to file pointer
  foreach ($data as $line) fputcsv($fp, $line);

  // Place stream pointer at beginning
  rewind($fp);

  // Return the data
  return stream_get_contents($fp);

}

 if ( isset( $_POST['submit'] ) ) {
function send_csv_mail ($csvData, $body, $to = 'shaun@bluemooncreative.co.uk', $subject = 'Test email with attachment', $from = 'webmaster@example.com') {

global $post;
$user_id = get_current_user_id();

  // This will provide plenty adequate entropy
  $multipartSep = '-----'.md5(time()).'-----';

  // Arrays are much more readable
  $headers = array(
    "From: $from",
    "Reply-To: $from",
    "Content-Type: multipart/mixed; boundary=\"$multipartSep\""
  );

  // Make the attachment
  $attachment = chunk_split(base64_encode(create_csv_string($csvData))); 

  // Make the body of the message
  $body = "--$multipartSep\r\n"
        . "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
        . "Content-Transfer-Encoding: 7bit\r\n"
        . "\r\n"
        . "$body\r\n"
        . "--$multipartSep\r\n"
        . "Content-Type: text/csv\r\n"
        . "Content-Transfer-Encoding: base64\r\n"
        . "Content-Disposition: attachment; filename=\"".date('Y-m-d')."-".str_replace(' ', '-', strtolower(get_user_meta($user_id, "wpcf-branch-active", true)))."-file.csv\"\r\n"
        . "\r\n"
        . "$attachment\r\n"
        . "--$multipartSep--";

   // Send the email, return the result
   return @mail($to, $subject, $body, implode("\r\n", $headers)); 


}



$array = array(
                array("Code", "Product Category", "Product Family", "Description", "Quantity", "Bay"),
                cart_items_array()

);

send_csv_mail($array, "Hello World!!!\r\n This is simple text email message.");
}
?>

我怎样才能得到它所以每个最终的数组看起来像这样?

$array = array(
                array("Code", "Product Category", "Product Family", "Description", "Quantity", "Bay"),
               array("product-name-1", "product-sku-1"), array('product-name-2', 'product-sku-2) etc 

);

0 个答案:

没有答案