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

时间:2014-04-10 16:09:09

标签: php xml arrays wordpress woocommerce

我尝试在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 Google Shopping Data Feed

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

1 个答案:

答案 0 :(得分:0)

你的内爆应用程序不起作用的原因是因为$ description是一个关联数组。我建议把它变成一个多维数组,这样你就可以内爆了。您可以使用以下代码执行此操作:

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