2013-05-12 104 views
0

我有以下代碼:

<?php 
      $array1 = array("The price of Mac"=>2000, "One year refundable"=>"Yes", "24/7 support"=>"Yes"); 
      foreach ($array1 as $index => $value) { 
       if(is_int($value)) { 
        echo "{$index} is ${$value}, "; 
       } else { 
        echo "{$index} is {$value}, "; 
       } 
      } 
      echo "</ br>"; 
     ?> 

但是$ 2000沒有執行露面,當我把一個空間,在該行

if(is_int($value)) { 
         echo "{$index} is $ {$value}, "; 
        } 

$ 2000現身,但如何輸出$ 2000年?

回答

7
echo "{$index} is \${$value}, "; 
+0

難道我們在這裏需要括號? – 2013-05-12 15:44:14

+0

@ Mr.Alien不,大括號是可選的。 – 2013-05-12 15:44:52

+0

雅這就是我認爲這裏沒有數組 – 2013-05-12 15:45:34

2

使用

echo $index. ' is $'.$value.', '; 

echo "{$index} is &#36;{$value}, "; 

echo "{$index} is \${$value}, "; 
+0

謝謝,在第二種解決方案中,「#36」是什麼意思? – user2131316 2013-05-13 08:28:40

+0

不客氣!這是美元符號的ASCII碼。如果您需要更多符號,請查看此表格:http://www.webmaster-knowhow.com/tabellen/html-ascii-codes-tabelle/ – 2013-05-16 04:36:03