我有一個多維數組,我試圖從中使用foreach
。PHP,獲取來自(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]
的值。但我認爲這對我來說是一個單獨的問題/挑戰。
我希望我已經明白上面。我不熟悉數組,並且在通過很多示例進行搜索時沒有發現任何數組與上面那樣複雜,或者具有唯一命名的部分。
API的陣列結構不會使-很大的意義。他們的文件是否有解釋? –
我知道!這是[WHMCS](http://whmcs.com)的API(計費'解決方案')。對於這樣的事情,他們的文檔並不太好。支持留下了許多不足之處。 –