将固定的xml添加到php foreach

时间:2015-05-22 14:19:54

标签: php arrays xml

我们正在尝试将固定值添加到数组的末尾,我们目前的代码是:

        foreach ($items as $k => $value) 
        {
            $xml['Contact']['Document']['Item'][$k]['Partcode'] = $value['sku'];
            $modifiers = '';
            if($modifiers_arr = json_decode($value['modifiers']))
            {
                foreach ($modifiers_arr as $mod_key => $mod_val) 
                {
                    $modifiers .= "{$mod_val->modifier_name} {$mod_val->modifier_value} ";
                    if(@$mod_val->price_mod)
                    {
                        $modifiers .= "{$mod_val->price_mod} ";
                    }
                }
            }
            $xml['Contact']['Document']['Item'][$k]['Description'] = "{$value['title']} {$modifiers}";
            $xml['Contact']['Document']['Item'][$k]['Quantity'] = "{$value['item_qty']}";
            $xml['Contact']['Document']['Item'][$k]['UnitPrice'] = "{$value['price']}";
            $xml['Contact']['Document']['Item'][$k]['UnitTax'] = round($value['item_tax'] / $value['item_qty'],2);
            $xml['Contact']['Document']['Item'][$k]['UnitNetPrice'] = $value['price'] - round(($value['item_tax'] / $value['item_qty']),2);
        }

哪个输出:

  <Item>
    <Partcode>GRF72</Partcode>
    <Description>Odour Eliminator   </Description>
    <Quantity>1</Quantity>
    <UnitPrice>4.99</UnitPrice>
    <UnitTax>0.83</UnitTax>
    <UnitNetPrice>4.16</UnitNetPrice>
  </Item>

但是我们想在此之后添加一个固定值(可以从数组返回无限量的项目),所以最终看起来像:

  <Item>
    <Partcode>GRF72</Partcode>
    <Description>Product title</Description>
    <Quantity>1</Quantity>
    <UnitPrice>4.99</UnitPrice>
    <UnitTax>0.83</UnitTax>
    <UnitNetPrice>4.16</UnitNetPrice>
  </Item>
  <Item>
    <Partcode>CAR01</Partcode>
    <Description>Delivery</Description>
    <Quantity>1</Quantity>
    <UnitPrice>4.99</UnitPrice>
    <UnitTax>0</UnitTax>
    <UnitNetPrice>4.99</UnitNetPrice>
  </Item>

尝试过在数组之后添加此代码,但它没有正确格式化,任何想法?

    $xml['Contact']['Document']['Item']['Partcode'] =  "CAR01";
    $xml['Contact']['Document']['Item']['Description'] = "Shipping";
    $xml['Contact']['Document']['Item']['Quantity'] =  "1";
    $xml['Contact']['Document']['Item']['UnitPrice'] =  $order_data['order_shipping'];
    $xml['Contact']['Document']['Item']['UnitTax'] = "0";
    $xml['Contact']['Document']['Item']['UnitNetPrice'] = $order_data['order_shipping'];

只需输出:

  <Item>
    <Partcode>GRF72</Partcode>
    <Description>Odour Eliminator   </Description>
    <Quantity>1</Quantity>
    <UnitPrice>4.99</UnitPrice>
    <UnitTax>0.83</UnitTax>
    <UnitNetPrice>4.16</UnitNetPrice>
  </Item>
  <Item>CAR01</Item>
  <Item>Shipping</Item>
  <Item>1</Item>
  <Item>0.00</Item>
  <Item>0</Item>
  <Item>0.00</Item>

2 个答案:

答案 0 :(得分:2)

您没有为固定值添加构建相同的结构:

$xml['Contact']['Document']['Item'][$k]['Description'] = "{$value['title']} {$modifiers}";
$xml['Contact']['Document']['Item']    ['Partcode'] =  "CAR01";
                                   ^^^^^---missing index

在第一行['Item']是一个数组数组,[$k]部分。您没有在固定添加中执行该子索引,这意味着您将文本“向上”添加到其他所有内容中。

基本上你正在制作

$xml = array(
   'Contact' => array(
      'Document' => array(
          'Item' => array
              0 => array(
                 'Description' => ... the stuff from your loop
                 etc...
              ),
              'Partcode' => array
                  etc...   fixed part here
              )
           )
        )
    )
)

答案 1 :(得分:2)

您当前只在$xml['Contact']['Document']['Item']词典中添加新的键值对,而您应该在那里添加新的词典。

您还需要为此新项定义一个键并添加该项。

$somekey = "somevalue";
$xml['Contact']['Document']['Item'][$somekey]['Partcode'] =  "CAR01";
$xml['Contact']['Document']['Item'][$somekey]['Description'] = "Shipping";
$xml['Contact']['Document']['Item'][$somekey]['Quantity'] =  "1";
$xml['Contact']['Document']['Item'][$somekey]['UnitPrice'] =  $order_data['order_shipping'];
$xml['Contact']['Document']['Item'][$somekey]['UnitTax'] = "0";
$xml['Contact']['Document']['Item'][$somekey]['UnitNetPrice'] = $order_data['order_shipping'];

$somekey的值取决于您的申请。