Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
The api's array structure doesn't make a-lot of sense. Does their documentation have an explanation ? –  William Smith Dec 12 '13 at 3:06
    
I know! It's WHMCS's API (the billing 'solution'). Their documentation isn't too great on things like this to be honest. And support leaves a lot to be desired. –  Elijah Paul Dec 12 '13 at 3:26

2 Answers 2

up vote 1 down vote accepted

First off, where did this $pricing variable come from? You were using $num at first.

And yes, just a nested foreach loop will do the trick like so:

$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;
    }

}
share|improve this answer
    
This works perfectly. Thank you. Am looking forward to analysing the elements of your answer. P.S. You're missing a closing ; at the end of echo "Options: \n". –  Elijah Paul Dec 14 '13 at 4:14
    
P.P.S. with the $pricing variable, I renamed some of my variables, and forgot to change them all back before pasting my code. Have update the question. –  Elijah Paul Dec 14 '13 at 4:15

First off, looking at the array structure, 'CUSTOMFIELDS' is just an empty key, it doesn't contain 'CONFIGOPTION'.

So this:

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

Needs to look like this:

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

I think I'd probably try to first fix all the names of the indexes and make my life easy from there. I would traverse the whole array with this function:

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;
        } 
    }
}

This function (Found it here: http://snipplr.com/view/10200/ and modified it for the current question) will go through the whole array, each level of it, and remove numbers from end of array keys IF the key without the number doesn't exist. Reason for that is some of those arrays contain multiple arrays with keys like OPTION, OPTION12, OPTION43, etc. If the key without the number within the same level exists, it will leave it alone. Now that takes all the numbers away from keys like PRICING, GBP, etc. We're only gonna be left with some OPTION keys that will have numbers at the end. Then we will be able to go:

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'];

}

Try it out. I think that should work.

P.S.: They should really figure something out on their end about that API because that's a horrible array to work with. There's no reason for that.

share|improve this answer
    
Sorry, I'm not 100% sure what you mean by this? –  Elijah Paul Dec 12 '13 at 4:09
    
I've actually just seen that the api allows me to pull the results in JSON format. So using json_decode($arr, true); I can format the results into a much friendlier associative array! I don't know why they (WHMCS) gave me the above as the main way to get the array! –  Elijah Paul Dec 12 '13 at 11:35
    
I seem to be receiving a syntax error, unexpected T_VARIABLE error with your function above. But I can't see where anything is missing in it. –  Elijah Paul Dec 12 '13 at 11:51
    
You were missing a closing ) after the second if. –  Elijah Paul Dec 12 '13 at 12:10
    
Ok. I tried again, but received an 'syntax error, unexpected T_STRING` error for the traverseArray($arr); line. –  Elijah Paul Dec 12 '13 at 12:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.