2014-09-28 200 views
-1

我有一個多維數組:打印陣列多維PHP

Array 
(
    [account] => Array 
     (
      [0] => Array 
       (
        [id] => 999856 
        [name] => Ice Cream 
        [description] => Array 
         (
         ) 

        [accountId] => 7527391 
        [gain] => 8.1302399863 
        [absGain] => 8.13 
        [daily] => 0.18 
        [monthly] => 5.58 
        [withdrawals] => 0.00 
        [deposits] => 100000.00 
        [interest] => -278.46 
        [profit] => 8130.24 
        [balance] => 108130.24 
        [drawdown] => 26.78 
        [equity] => 102768.19 
        [demo] => true 
        [lastUpdateDate] => 09/26/2014 20:48 
        [creationDate] => 08/17/2014 21:00 
        [firstTradeDate] => 08/16/2014 00:00 
        [tracking] => 0 
        [views] => 3 
        [commission] => 0.00 
        [currency] => USD 
        [profitFactor] => 1.36 
        [pips] => -291.5 
        [invitationUrl] => Array 
         (
         ) 

        [server] => Array 
         (
          [name] => Other(MT4) 
         ) 

       ) 

      [1] => Array 
       (
        [id] => 1030189 
        [name] => Vanilla CupCake 
        [description] => Array 
         (
         ) 

        [accountId] => 7276363 
        [gain] => 44.7620800233 
        [absGain] => 44.76 
        [daily] => 0.07 
        [monthly] => 2.23 
        [withdrawals] => 0.00 
        [deposits] => 100000.00 
        [interest] => -1837.49 
        [profit] => 44762.08 
        [balance] => 144762.08 
        [drawdown] => 12.43 
        [equity] => 144762.08 
        [demo] => true 
        [lastUpdateDate] => 09/26/2014 20:51 
        [creationDate] => 09/26/2014 16:19 
        [firstTradeDate] => 05/12/2013 00:00 
        [tracking] => 0 
        [views] => 1 
        [commission] => 0.00 
        [currency] => USD 
        [profitFactor] => 1.37 
        [pips] => -4302.1 
        [invitationUrl] => Array 
         (
         ) 

        [server] => Array 
         (
          [name] => Other(MT4) 
         ) 

       ) 

     ) 

) 

我需要去並打印的值[0]和[1]用foreach只打印的值[0] 。

//the foreach 
<?php 
foreach($real as $r => $val){ 
echo $val['id']; 
echo $val['name']; 
echo $val['accountId']; 
echo $val['gain']; 
echo $val['daily']; 
echo $val['monthly']; 
echo $val['balance']; 

?> 

此foreach僅打印值爲[0] ...爲什麼不循環數組[1]?

+1

能否請你分享'的foreach()'? – loveNoHate 2014-09-28 04:52:00

+0

你爲什麼「需要」這樣做? – 2014-09-28 04:52:06

回答

0

您需要3個嵌套for循環。內一個將鍵值對

如果$ a是你的變量名,然後

foreach($a as $b) 
{ 
    foreach($b as $val) 
    { 
    foreach($val as $key=>$vl) 
    { 
     echo $key.' '. $vl; 
    } 
    } 
} 
1

你可以試試這個:

foreach($data['account'] as $key=>$val){ 
    echo $val['id']; 
    echo $val['name']; 
}