I have a multi dimensional array from which I'm trying to pull values from using foreach
.
It's retrieved JSON format, so I've used json_decode
to convert it to an associative array.
The array contains pricing information for various products. The problem I'm struggling with is that it contains multiple nested arrays for each product.
Because of this, I'm not sure how I integrate this into foreach
.
Array $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
)
)
)
)
)
)
)
)
)
The above section is repeated from [product]
for each product ID ([pid]
) - I couldn't fit the whole array in the post, so here's a link to it - http://pastebin.com/0qgh5scG
What I want to achieve is pulling the name description, and EACH Monthly* price for each product ID [pid]
in the array, into an array of it's own with that arrays variable name being the associated [pid]
.
*(The monthly price is actually an annual price, it's just a weird manner in which the module stores the data in the database)
I've experimented with 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;
}
The product ID, retrieves ok, but how on earth do I pull the multiple [name]
and related multiple [monthly]
values for each product ID [pid]
?
Do I require a foreach
within in my existing foreach
?
Eventually I want to pass these values to new arrays, with each array named after it's corresponding [pid]
value. But I think that's a separate question/challenge for me.
I hope I've made sense above. I'm new to arrays, and in searching through quite a few examples didn't find any that had an array as complex as the one above, or that had sections that are uniquely named.