0

我有以下代码用于获取 XML 并制作数组,以便可以通过 API 进行更新:

$data1 = file_get_contents('neworderexample.xml');
$xml = new SimpleXMLElement($data1,LIBXML_NOCDATA);

foreach($xml -> Product as $ord){
$sku = $ord -> StockNumber;
$title = $ord -> Title;
$retail_price = $ord -> RRP;
$price = $ord -> SellPrice;
$description = $ord -> Description;
$inventory_level = $ord -> StockLevel;
$weight = $ord -> Weight;
$width = $ord -> Width;
$prodheight = $ord -> Height;
$depth = $ord -> Depth;

$fields = array(
"sku" => $sku,
"name" => $title,
"retail_price" => $retail_price,
"price" => $price,
"description " => $description,
"inventory_level " => $inventory_level,
"type" => "physical",
"availability" => "available",
"weight" => $weight,
"width" => $width,
"prodheight" => $prodheight,
"depth" => $depth,
"categories" => "1"
);

print_r ($fields);  

当我回显任何变量时,我只得到值,但是当我打印数组时,我得到以下内容:

[inventory_level ] => SimpleXMLElement Object ( [0] => 9 ) 
[type] => physical 
[availability] => available 
[weight] => SimpleXMLElement Object ( [@attributes] => Array ( [unit] => g ) [0] => 0.0 ) 
[width] => SimpleXMLElement Object ( [@attributes] => Array ( [unit] => (mm) ) [0] => 0 ) 
[prodheight] => SimpleXMLElement Object ( [@attributes] => Array ( [unit] => (mm) ) [0] => 0 ) 
[depth] => SimpleXMLElement Object ( [@attributes] => Array ( [unit] => (mm) ) [0] => 0 ) 
[categories] => 1 ) 

如何仅获取数组中的值?

4

2 回答 2

1

转换为字符串,例如使用:

"sku" => (string) $sku,
         ^ ^ ^ ^ 

(这更像是一个评论而不是一个答案,它将与重复的问题一起被删除)

于 2012-04-25T15:35:45.233 回答
0

问题出现在 SimpleXMLElement 创建对象的事实中。尝试将这些特定元素转换为整数/浮点数/字符串。

前任。

$fields = array(
"sku" => intval($sku),
"name" => $title,
"retail_price" => $retail_price,
"price" => $price,
"description " => $description,
"inventory_level " => $inventory_level,
"type" => "physical",
"availability" => "available",
"weight" => floatval($weight[0]).$weight['unit'],
"width" => intval($width[0]).$width['unit'],
"prodheight" => intval($prodheight[0]).$prodheight['unit'],
"depth" => intval($depth[0]).$depth['unit'],
"categories" => "1"
);

尝试它可能会起作用。我不太确定自己没有进行任何测试。

于 2012-04-25T15:50:36.350 回答