如何以纯字符串输出整个php数组以用于XML


How to output whole php array in plain string to use in XML

我试图在php中输出一个简单的数组,将其用于我的谷歌购物数据源(XML),但不幸的是,它并没有那么简单。。。

这是我想要输出的数组:

$description = $post->post_content;

这是我商店中Woocommerce产品的产品描述。除产品描述外,所有详细信息(如定价和简短描述)都以字符串形式出现。这是一个有单个字母的数组。我可以输出它们,比如:

$description[0] = (first letter in description)
$description[1] = (next letter... and so on...)

所以我的问题是:我如何才能一次将它们回声出来

这是我的代码:

<?php
require_once("../../../wp-load.php");
// Set the xml header
header('Content-Type: text/xml; charset=UTF-8');
// Echo out all the details
echo '<?xml version="1.0" encoding="UTF-8"?> 
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>M13 Shop Feed</title>
<link>http://www.manufaktur13.de</link>
<description>Google Merchant Feed</description>';
$args = array(
    'post_type' => 'product',
    'product_cat' => 'tabak-taschen , accessoires',
    'orderby' => '_sku'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
        $product = get_product($loop->post);
        $title = $product->get_title();
        $link = get_permalink();
        $description = $post->post_content; // <--this is the strange array
        $test = implode(' ', $description ); // <--testing implode, not working :(
        $sku = $product->get_sku();
        $price = $product->price;
        $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));

        echo "<item> 
<title>$title</title>
<link>$link</link>
// Testing the variables in description to see if they are correct 
<description>$price €, $sku , $image[0] , $description[0]$description[1]$description[2]$description[3]$description[4]$description[5] $test</description>
<g:google_product_category>Heim &amp; Garten &gt; Rauchzubehör</g:google_product_category>
<g:id>$sku</g:id>
<g:condition>Neu</g:condition>
<g:price>$price €</g:price>
<g:availability>Auf Lager</g:availability>
<g:image_link>$image[0]</g:image_link>
<g:shipping>
    <g:country>DE</g:country>
    <g:service>Standard</g:service>
    <g:price>0.00 €</g:price>
</g:shipping>
<g:gtin></g:gtin>
<g:brand>Manufaktur13</g:brand>
<g:mpn></g:mpn>
<g:product_type>Tabaktasche</g:product_type>
<g:identifier_exists>false</g:identifier_exists>
</item>";
endwhile;
wp_reset_query();
echo "</channel>
</rss>";
?>

示例:XML谷歌购物数据馈送

最后一个问题:为什么有一个充满单个字母的数组?

内爆应用程序不工作的原因是$description是一个关联数组。我建议把它做成一个多维数组,这样你就可以内爆了。你可以用这个代码这样做:

foreach ($description as $key => $value) {
    $description_array[] = $value;
}
$description_string = implode(" ", $description_array);