I'm trying to extract values from the following array into a new array:
I received help with the foreach
command, but am now having trouble combining two arrays, one of which has null
values.
The original array is: http://pastebin.com/jeKVxpka
Using the following:
foreach ($arr['products']['product'] as $num) {
$pid = $num['pid'];
foreach($num['configoptions']['configoption']['0']['options']['option'] as $option)
{
$name = $option['name'];
$yearlycosts = $option['pricing']['GBP']['monthly'];
$namearr[] = $name;
$yearlyarr[] = $yearlycosts;
$peryear = array_combine($namearr, $yearlyarr);
}
$pricing[$pid] = array('cost' => $peryear);
}
print_r($pricing);
I've been able to produce the following array:
Array
(
[2] => Array
(
[cost] => Array
(
[1 Year] => 4.17
[2 Years] => 8.33
[3 Years] => 12.50
[4 Years] => 16.67
[5 Years] => 20.83
)
)
[3] => Array
(
[cost] => Array
(
[1 Year] => 40.83
[2 Years] => 8.33 <---
[3 Years] => 12.50 |--- These values shouldn't be here!
[4 Years] => 16.67 |
[5 Years] => 20.83 <---
)
)
[4] => Array
(
[cost] => Array
(
[1 Year] => 49.17
[2 Years] => 79.17
[3 Years] => 115.83
[4 Years] => 149.17
[5 Years] => 190.83
)
)
)
The problem is, if you look at the original array, there is no price information for product ID [3]
. Years 2 to 5 have been populated with the values from years 2 to 5 for product ID [2]
.
I've experimented with array_filter
and unset
, but I can't figure out how to prevent this from happening (or why it is happening).