【问题标题】:How to output whole php array in plain string to use in XML如何以纯字符串输出整个 php 数组以在 XML 中使用
【发布时间】:2014-05-24 11:22:49
【问题描述】:

我尝试在 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

最后一个问题:为什么会有一个全是单个字母的数组?

【问题讨论】:

  • var_dump($description); 会得到什么?
  • xml 停止工作(灰色空表)。我也试过了,print_r 但是xml每次都会中断(没有错误消息)
  • 您可能必须将部分代码隔离在单独的文件中以进行测试。此外,xml 可能会中断,因为您没有关闭标签 - 例如,我在任何地方都看不到 &lt;/channel&gt; 标签。
  • 抱歉,忘记正确复制和粘贴。我用 isarray 检查了 $description 并且 var 不是一个数组,所以我认为问题出在其他地方但是当我在一个简单的 html(header: text/html) 输出中测试它的输出时,它会正确显示并且整个产品描述立即出现

标签: php xml arrays wordpress woocommerce


【解决方案1】:

您的 implode 应用程序不工作的原因是因为 $description 是一个关联数组。我建议将其设为多维数组,以便您可以内爆。您可以使用以下代码执行此操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多