2015-07-10 60 views
2

回波值我有:PHP - 如何從多維數組

$request = Array 
(
    [ID] => 2264 
    [SUCCESS] => Array 
     (
      [0] => Array 
       (
        [MESSAGE] => Service Details 
        [LIST] => Array 
         (
          [retail_name] => 
          [credit_admin] => FREE 
          [credit] => 0 
          [discount_admin] => 0 
          [discount] => 0 
          [cartdiscount] => 0 

如果我做的:

echo $request[ID];       // it says: 2264 
echo $request[SUCCESS];      // it says: Array 
echo $request[SUCCESS][0][MESSAGE];   // it says: Service Details 

但我需要回聲 「信用」,如果我做的:

echo $request[SUCCESS][0][LIST];   // I get ERROR 
echo $request[SUCCESS][0][LIST][credit]; // I get ERROR 

我不明白爲什麼?我該怎麼做? 謝謝

+5

嘗試爲'回聲$請求[「成功」] [0] [「列」] [「信用」]做; '如果它不會工作發佈你的錯誤 –

+0

檢查這個帖子它可能會幫助你調試它http://stackoverflow.com/questions/13018280/manipulating-data-from-a-json-feed-to-display-它 –

+1

@Uchiha謝謝你,工作!嗯..所以總是應該使用'? – Cyborg

回答

0

您不使用引號來指定數組鍵。你應該使用的['ID']代替[ID]

PHP修復了這個爲你和你假定在日誌中

這doen't工作意味着['ID'],而不是[ID]和trows通知爲[LIST]但因爲LIST是保留關鍵字。這意味着list在PHP中有一個函數。 PHP不知道你需要哪一個,也不會返回結果。

更改[LIST]['LIST']並且您應該收到您的值。 請學會使用數組加上引號,以防止類似的錯誤在未來

+0

** List **並不是一個真正的函數,而是一種語言結構。['list()'](http://php.net/manual/en/function.list.php)在一個操作中分配一個變量列表。 –

+0

@Uchiha:這就是爲什麼我提到它在PHP中有一個函數,而不是它是PHP中的函數;) – AgeDeO

0

取決於你KeyValue

如果陣列鍵都有一個值就會直接輸出

爲前

echo $request[ID]; 

b'coz ID直接持有的值;

$request[SUCCESS]; 

// holds an array. you cannot echo a array directly 
// to do something like this you will need the key of your inner 
// array with outer array in combinations to echo the values 

爲前

echo $request[SUCCESS][0]['MESSAGE']; 

//鍵 「O」 持有陣列也。所以你需要使用它的內部數組鍵

相同需求

echo $request[SUCCESS][0]['MESSAGE']['LIST']['credit_admin']; 
// where again list is a array