duanpi7107 2014-04-10 16:09
浏览 67

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

I try to output a simple array in php to use it for my google shopping data feed(XML) but unfortunatly its not that simple...

This is the array I want to output:

$description = $post->post_content;

It's the Product description of a Woocommerce product in my Shop. All Details like pricing and short description come in a string except the product description. It's an array with single letters in it. I can output them like:

$description[0] = (first letter in description)

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

So my question is: How can I echo them out at once?

here's my code:

<?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>";

?>

LIVE-EXAMPLE: XML Google Shopping Data Feed

Last question: Why is there an array full of single letters?

  • 写回答

1条回答 默认 最新

  • dongyanzhui0524 2014-04-10 16:42
    关注

    The reason your implode application isn't working is because $description is an associative array. I would recommend making it a multidimensional array so you could implode. You would do so with this code:

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

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄