2013-12-12 69 views
0

我有一個多維數組,我試圖從中使用foreachPHP,獲取來自(Mutli)多維數組的值

它被檢索到JSON格式,所以我用json_decode將它轉換爲關聯數組。

該陣列包含各種產品的價格信息。我正在努力解決的問題是它包含每個產品的多個嵌套數組。

因此,我不知道如何將其整合到foreach

數組$ ARR:

Array 
(
    [result] => success 
    [totalresults] => 3 
    [products] => Array 
     (
      [product] => Array 
       (
        [0] => Array 
         (
          [pid] => 2 
          [gid] => 2 
          [type] => other 
          [name] => Shared Hosting 
          [description] => Shared Cloud 
          [module] => custom server 
          [paytype] => onetime 
          [pricing] => Array 
           (
            [GBP] => Array 
             (
              [prefix] => £ 
              [suffix] => GBP 
              [msetupfee] => 0.00 
              [qsetupfee] => 0.00 
              [ssetupfee] => 0.00 
              [asetupfee] => 0.00 
              [bsetupfee] => 0.00 
              [tsetupfee] => 0.00 
              [monthly] => 0.00 
              [quarterly] => -1.00 
              [semiannually] => -1.00 
              [annually] => -1.00 
              [biennially] => -1.00 
              [triennially] => -1.00 
             ) 

           ) 

          [customfields] => Array 
           (
            [customfield] => Array 
             (
             ) 

           ) 

          [configoptions] => Array 
           (
            [configoption] => Array 
             (
              [0] => Array 
               (
                [id] => 2 
                [name] => Years 
                [type] => 2 
                [options] => Array 
                 (
                  [option] => Array 
                   (
                    [0] => Array 
                     (
                      [id] => 2 
                      [name] => 1 Year 
                      [recurring] => 
                      [pricing] => Array 
                       (
                        [GBP] => Array 
                         (
                          [msetupfee] => 0.00 
                          [qsetupfee] => 0.00 
                          [ssetupfee] => 0.00 
                          [asetupfee] => 0.00 
                          [bsetupfee] => 0.00 
                          [tsetupfee] => 0.00 
                          [monthly] => 69.00 
                          [quarterly] => 0.00 
                          [semiannually] => 0.00 
                          [annually] => 0.00 
                          [biennially] => 0.00 
                          [triennially] => 0.00 
                         ) 

                       ) 

                     ) 

                    [1] => Array 
                     (
                      [id] => 5 
                      [name] => 2 Years 
                      [recurring] => 
                      [pricing] => Array 
                       (
                        [GBP] => Array 
                         (
                          [msetupfee] => 0.00 
                          [qsetupfee] => 0.00 
                          [ssetupfee] => 0.00 
                          [asetupfee] => 0.00 
                          [bsetupfee] => 0.00 
                          [tsetupfee] => 0.00 
                          [monthly] => 138.00 
                          [quarterly] => 0.00 
                          [semiannually] => 0.00 
                          [annually] => 0.00 
                          [biennially] => 0.00 
                          [triennially] => 0.00 
                         ) 

                       ) 

                     ) 

                    [2] => Array 
                     (
                      [id] => 8 
                      [name] => 3 Years 
                      [recurring] => 
                      [pricing] => Array 
                       (
                        [GBP] => Array 
                         (
                          [msetupfee] => 0.00 
                          [qsetupfee] => 0.00 
                          [ssetupfee] => 0.00 
                          [asetupfee] => 0.00 
                          [bsetupfee] => 0.00 
                          [tsetupfee] => 0.00 
                          [monthly] => 276.00 
                          [quarterly] => 0.00 
                          [semiannually] => 0.00 
                          [annually] => 0.00 
                          [biennially] => 0.00 
                          [triennially] => 0.00 
                         ) 

                       ) 

                     ) 

                    [3] => Array 
                     (
                      [id] => 11 
                      [name] => 4 Years 
                      [recurring] => 
                      [pricing] => Array 
                       (
                        [GBP] => Array 
                         (
                          [msetupfee] => 0.00 
                          [qsetupfee] => 0.00 
                          [ssetupfee] => 0.00 
                          [asetupfee] => 0.00 
                          [bsetupfee] => 0.00 
                          [tsetupfee] => 0.00 
                          [monthly] => 552.00 
                          [quarterly] => 0.00 
                          [semiannually] => 0.00 
                          [annually] => 0.00 
                          [biennially] => 0.00 
                          [triennially] => 0.00 
                         ) 

                       ) 

                     ) 

                    [4] => Array 
                     (
                      [id] => 14 
                      [name] => 5 Years 
                      [recurring] => 
                      [pricing] => Array 
                       (
                        [GBP] => Array 
                         (
                          [msetupfee] => 0.00 
                          [qsetupfee] => 0.00 
                          [ssetupfee] => 0.00 
                          [asetupfee] => 0.00 
                          [bsetupfee] => 0.00 
                          [tsetupfee] => 0.00 
                          [monthly] => 1104.00 
                          [quarterly] => 0.00 
                          [semiannually] => 0.00 
                          [annually] => 0.00 
                          [biennially] => 0.00 
                          [triennially] => 0.00 
                         ) 

                       ) 

                     ) 

                   ) 

                 ) 

               ) 

             ) 

           ) 

         ) 

以上部分由[product]重複每個產品ID([pid]) - 我可能不適合在後整個數組,所以這裏的一個鏈接 - http://pastebin.com/0qgh5scG

我想要實現的是將數組中的每個產品ID [pid]的名稱描述和EACH Monthly *價格拉到它自己的數組中,該數組變量名稱是關聯的[pid]

*(每月的價格實際上是年度價格,它只是一個奇怪的方式在該模塊存儲在數據庫中的數據)

我已經與foreach試驗:

$arr = json_decode($jsondata, true); # Decode JSON String 

foreach ($arr['products']['product'] as $num) { 

    $pid = $num['pid']; 

    $yearlycosts = $arr['configoptions']['configoption']['0']['options']['option'][0]['pricing']['GBP']['monthly']; 

    echo $pid; 
    echo $yearlycosts; 
} 

的產品ID,檢索正常,但我怎樣才能拉出多個[name]和相關的多個[monthly]值爲每個產品ID [pid]

我需要foreach在我現有的foreach範圍內嗎?

最終,我想將這些值傳遞給新數組,每個數組的值都對應於[pid]的值。但我認爲這對我來說是一個單獨的問題/挑戰。

我希望我已經明白上面。我不熟悉數組,並且在通過很多示例進行搜索時沒有發現任何數組與上面那樣複雜,或者具有唯一命名的部分。

+0

API的陣列結構不會使-很大的意義。他們的文件是否有解釋? –

+0

我知道!這是[WHMCS](http://whmcs.com)的API(計費'解決方案')。對於這樣的事情,他們的文檔並不太好。支持留下了許多不足之處。 –

回答

1

首先,這個$定價變量從哪裏來?您首先使用$ num。

是的,只是一個嵌套的foreach循環就可以了,像這樣:

$arr = json_decode($jsondata, true); # Decode JSON String 

foreach ($arr['products']['product'] as $num) { 

    $pid = $num['pid']; 

    echo "Product ID: ".$pid."\n"; 
    echo "Options: \n" 

    $i = 1; 

    foreach($num['configoptions']['configoption']['0']['options']['option'] as $option) 
    { 
     $name = $option['name']; 
     $yearlycosts = $option['pricing']['GBP']['monthly']; 

     echo " - Option ".$i.": ".$name." ($".$yearlycosts.")\n"; 

     ++$i; 
    } 

} 
+0

這很好。謝謝。我期待着分析你答案的內容。附:你在'echo'選項結尾缺少一個關閉';'''\ n「'。 –

+0

P.P.S.使用'$ pricing'變量時,我重命名了一些變量,並忘記在粘貼我的代碼之前全部更改它們。有更新的問題。 –

0

首先,看看數組結構,'CUSTOMFIELDS'只是一個空的鍵,它不包含'CONFIGOPTION'。

所以這個:

echo $k['CUSTOMFIELDS'][CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][NAME]; 
echo $k['CUSTOMFIELDS'][CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][MONTHLY]; 

需要看起來像這樣:

echo $k[CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][NAME]; 
echo $k[CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][MONTHLY]; 

我想我可能會嘗試先修復指標的所有的名字,讓我的生活更輕鬆從那裏。我會遍歷整個陣列使用此項功能:

function traverseArray($array) 
{ 
    // Loops through each element. If element again is array, function is recalled. If not, result is echoed. 
    foreach($array as $key=>$value) 
    { 
     if (is_array($value)) 
     { 
      traverseArray($value); 
     } else { 
      if (preg_match("/[0-9]/", $key) && !isset($array[preg_replace("[0-9]", "", $key)])) 
         $array[preg_replace("[0-9]", "", $key)] = $value; 
     } 
    } 
} 

此功能(在這裏找到它:http://snipplr.com/view/10200/,並修改了它當前的問題)將經過整個陣列,它的每一個層面,並從最終刪除號碼數組鍵如果沒有數字的鍵不存在。原因是這些數組中的某些數組包含多個數組,其中包含OPTION,OPTION12,OPTION43等鍵。如果不存在同一級別中的數字的鍵不存在,則它將保持獨立。現在,將所有數字從諸如定價,英鎊等的鍵中移走。我們只剩下一些OPTION鍵,最後會有數字。然後我們將能夠去:

traverseArray($arr); // Goes through arrray and removes random numbers unless not possible 

foreach($k['CONFIGOPTIONS']['CONFIGOPTION']['OPTIONS'] as $option) { 

    echo $option['NAME']; 
    echo $option['PRICING']['GBP']['MONTHLY']; 

} 

試一試。我認爲這應該工作。

P.S .:他們應該真的在他們的API上找出一些事情,因爲這是一個可怕的陣列。這沒有理由。

+0

對不起,我不是100%確定你的意思是? –

+0

我剛剛看到api允許我以JSON格式提取結果。因此,使用'json_decode($ arr,true);'我可以將結果格式化爲更友好的關聯數組!我不知道他們爲什麼(WHMCS)給了我上面的這個數組的主要方法! –

+0

我似乎在上面的函數中收到'語法錯誤,意外的T_VARIABLE'錯誤。但是我看不到它裏面缺少什麼東西。 –