2012-04-03 3 views
0

私のタイトル(ヘブン)のパンチが、これは真剣に私ナットを運転している! これは私のコードです:アレイドライブ私のループ(y)

for ($i=0;$i < $a;$i++){ 

    $total = (array)$orders -> Total -> Line[$i]; 

    echo '<pre>'; 
    print_r($total); 
    echo '</pre>'; 
} 

...それは次のように出力します

Array 
(
    [@attributes] => Array 
     (
      [type] => Subtotal 
      [name] => Subtotal 
     ) 

    [0] => 299.99 
) 

Array 
(
    [@attributes] => Array 
     (
      [type] => Shipping 
      [name] => Shipping 
     ) 

    [0] => 13.36 
) 

Array 
(
    [@attributes] => Array 
     (
      [type] => Tax 
      [name] => Tax 
     ) 

    [0] => 0.00 
) 

Array 
(
    [@attributes] => Array 
     (
      [type] => GiftCertificate 
      [name] => Gift certificate discount (117943:@CAC7HXPXFUNNJ3MTGC:63.35 117372:@DK9T9TMTCTCTUWF9GC:250.00) 
     ) 

    [0] => -313.35 
) 

Array 
(
    [@attributes] => Array 
     (
      [type] => Total 
      [name] => Total 
     ) 

    [0] => 0.00 
) 

私の質問は:どのように私はに従って命名それぞれの変数に[0]それぞれの金額を保存しませんarray ['type']?

+0

は 'SimpleXML'要素からこのですか? –

+0

はいロック...なぜですか?それはオブジェクトとしてより良いでしょうか? – bobbiloo

+0

元のXMLを表示できますか? SimpleXML要素は配列に変換せずに 'foreach'を使って繰り返し処理できます。 –

答えて

0

$var[] = array('type' => $total['@attributes']['type'], 'amount' => $total[0])

0

多分このような何か?

$total_amount_by_type = array(); 
for ($i=0;$i < $a;$i++){ 

    $total = (array)$orders -> Total -> Line[$i]; 

    $total_amount_by_type[$total->type] = $total[0] 

} 
3

ではなく(変数の変数で行うことができる)変数、私はtype属性をキー配列$prices、にそれらを置くことをお勧めします。

$prices = array(); 
for ($i=0;$i < $a;$i++){ 

    $total = (array)$orders -> Total -> Line[$i]; 

    echo '<pre>'; 
    print_r($total); 
    echo '</pre>'; 

    // Append the price to an array using its type attribute as the 
    // new array key 
    $prices[$total['@attributes']['type']] = $total[0]; 
} 

もちろん、私はそれが仕事をすると信じています。

0

何かlike thisを探しています:

for ($i=0;$i < $a;$i++){ 
    $total = (array)$orders -> Total -> Line[$i]; 

    // will create variables $Tax, $GiftCertificate etc 
    ${$total['@attributes']['type']} = $total[0]; 

    echo '<pre>'; 
    print_r($total); 
    echo '</pre>'; 
} 
関連する問題