0

私のタイトルの駄洒落を許してください(笑)が、これは私を真剣に駆り立てています!これは私のコードです:

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']に従って名前が付けられたそれぞれの変数に保存するにはどうすればよいですか?

4

4 に答える 4

3

$prices変数(変数変数を使用して実行できます)ではなく、属性でキー設定された配列にそれらを配置することをお勧めしtypeます。

$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];
}

もちろん、テストされていませんが、私はそれが仕事をするだろうと信じています。

于 2012-04-03T20:22:58.163 に答える
0

あなたはこのようなものを探していますか:

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>';
}
于 2012-04-03T20:27:52.853 に答える
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]

}
于 2012-04-03T20:22:51.013 に答える
0

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

于 2012-04-03T20:21:59.197 に答える