从带有对象的多维数组中获取值



我使用了一个小脚本来导入mt940文件(荷兰银行交易的文件)。

这是我得到的数组,但我需要一些帮助才能从数组中获得正确的值。

Array (
    [0] => Statement_banking Object (
        [_bank:Statement_banking:private] => ING
        [_account:Statement_banking:private] => 111111111
        [_transactions:Statement_banking:private] => Array (
            [0] => Transaction_banking Object (
                [account:Transaction_banking:private] => 111111111
                [accountName:Transaction_banking:private] => V. DE JONG KERKSTRAAT 1154 1234 BW
                [price:Transaction_banking:private] => 0.56
                [debitcredit:Transaction_banking:private] => C
                [description:Transaction_banking:private] => 0111111111 V. DE JONG KERKSTRAAT 1154 1234 BW ENSCHEDE BET.KENM. 1004510036716378 3305330802 AFLOSSINGSTERMIJN 188616 / 1E TERMIJN
                [valueTimestamp:Transaction_banking:private] => 1279749600
                [entryTimestamp:Transaction_banking:private] => 1279749600
                [transactionCode:Transaction_banking:private] => 078
            )
            [1] => Transaction_banking Object (
                [account:Transaction_banking:private] => 111111111 
                [accountName:Transaction_banking:private] => CUSTOMER NL SPOEDBETALING 
                [price:Transaction_banking:private] => 10.45 
                [debitcredit:Transaction_banking:private] => C 
                [description:Transaction_banking:private] => 0111111111 CUSTOMER NL SPOEDBETALING GE2009120212345 RE091202­3737 /RFB/NL­FMI­021209 NL­FMI­021209 VOORSCHOT COMMISSIE
                [valueTimestamp:Transaction_banking:private] => 1279749600 
                [entryTimestamp:Transaction_banking:private] => 1279749600 
                [transactionCode:Transaction_banking:private] => 077
            )
            [2] => Transaction_banking Object (
                [account:Transaction_banking:private] => 
                [accountName:Transaction_banking:private] => 
                [price:Transaction_banking:private] => 10000.99 
                [debitcredit:Transaction_banking:private] => D 
                [description:Transaction_banking:private] => VERZAMELBETALING BATCH­ID: 012345 TOTAAL 198 POSTEN 
                [valueTimestamp:Transaction_banking:private] => 1279749600 
                [entryTimestamp:Transaction_banking:private] => 1279749600 
                [transactionCode:Transaction_banking:private] => 029 ) )
                [_startPrice:Statement_banking:private] => 44 
                [_endPrice:Statement_banking:private] => 9945 
                [_timestamp:Statement_banking:private] => 1279749600 
                [_number:Statement_banking:private] => 100
            )
        )

例如,这个密钥给了我名称和地址:

[accountName:Transaction_banking:private] => V. DE JONG KERKSTRAAT 1154 1234 BW

这把钥匙给了我交易价格:

[price:Transaction_banking:private] => 0.56

有什么建议吗?

通常情况下,您会将地址和交易价格访问为:

foreach( $yourArray[0]->_transactions as $transaction ) {
    $foo = $transaction->accountName;
    $bar = $transaction->price;
}

但在这种特殊情况下,你的一些字段是私有的,这意味着你不能直接访问它们的值。您可以尝试定义自己的Statement_bankingTransaction_banking类,并为这些字段定义访问器方法,例如:

public class Statement_banking {
    public function getTransactions() {
        return $this->_transactions;
    }
}
public class Transaction_banking {
    public function getAccountName() {
        return $this->accountName;
    }
    public function getPrice() {
        return $this->price;
    }
}

然后你的循环代码变成这样:

foreach( $yourArray[0]->getTransactions() as $transaction ) {
    $foo = $transaction->getAccountName();
    $bar = $transaction->getPrice();
}

如果您也想在外部数组上进行迭代,可以将其扩展为:

foreach( $yourArray as $statement ) {
    foreach( $statement->getTransactions() as $transaction ) {
        $foo = $transaction->getAccountName();
        $bar = $transaction->getPrice();
    }
}

从逻辑上讲,您需要使用嵌套的foreach或for循环,不确定语法,但下面的PHP代码可能会帮助您,然后您可以转换为所需的语言

  $outputArr = Is your array.
  foreach ( $outputArr as $firstchild){
   forach ( $firstchild as $key => $secondchildvalue){
     // $key is basically your key value [price:Transaction_banking:private]
     // $secondchildvalue is your info value 0.56
   }
  }

相关内容

  • 没有找到相关文章

最新更新