0

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).

2
  • Why are you doing array_combine each time through the loop, instead of when the loop is done? Commented Dec 14, 2013 at 9:04
  • That seems so obviously wrong now. Thank you. Commented Dec 14, 2013 at 9:15

1 Answer 1

2

You need to empty the arrays before the inner loops, so you don't keep the values from the previous iteration of the main loop. And you should do the array_combine outside the inner loop.

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

    $pid = $num['pid'];

    $namearr = array();
    $yearlyarr = array();

    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);
Sign up to request clarification or add additional context in comments.

3 Comments

This works perfectly. Thank you. Since I'm new to arrays, I assumed that they would be empty since they haven't been defined yet.
They are empty before they're defined. But after the first time through the loop, they're defined, so they're not empty any more. Variables in PHP are not local to a particular loop; variable scoping in PHP is by function or class, not finer-grained blocks.
I see. That's helped me better understand why my code wasn't working as expected. Thanks again, appreciate the help (and explanations).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.