WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Retrieving value from a multidimensional array (5 posts)

  1. sysins
    Member
    Posted 1 year ago #

    Hello People,

    I need to retrieve a value from a multidimensional array.

    The array can be found here:
    http://pastebin.com/MJmRGEuF

    What I need to do is to retrieve the "amount" based upon the users role.

    i.e gold_membership = 50 etc...

    Sorry but I have no idea where to begin.

  2. bcworkz
    Member
    Posted 1 year ago #

    Holy nested arrays batman!
    I think this is right:

    <?php
    $role = 'gold_membership';
    foreach ($main_array as $set){
    	if ($set["conditions"][0]["args"]["roles"][0] == $role){
    		$amount = $set["rules"][0]["amount"];
    		break;
    	}
    }
    ?>

    $main_array is the array that contains all the elements similar to "set_5" and "set_6" etc. Note that $amount is a string representation of a number.

    I don't know of an easy way to reverse a var_dump or I would have tested this. If it doesn't work, var_dump the specific values with the series of square brackets, except dropping the last bracket set. If the reason for my error doesn't show up, drop the next last set until things make sense.

    If all else fails, serialize the array and post that, then I'll debug my code for you. It's also possible to build a friendlier array instead of returning a single value if that helps. The array will look like

    array('gold_membership'=>'50',
       'silver_membership'=>'25',
       'bronze_membership'=>'10');

    Use this:

    <?php
    $friendly = array();
    foreach ($main_array as $set){
       $friendly[$set["conditions"][0]["args"]["roles"][0]] = $set["rules"][0]["amount"];
    }
    ?>

    `

  3. sysins
    Member
    Posted 1 year ago #

    bcworkz You my friend are a legend!!!!

    Thank you very much. That worked for me.

    I am interested now though how to return a friendlier array in the first place as you suggested.

    I'm currently calling the data using

    $main_array = get_option('_s_membership_pricing_rules'); from the wp_options table in the database.

    The Raw data looks like this: http://pastebin.com/iTiaQDmi

    For anyone else who might be interested and stuck. This code is to get the logged in user and the percentage discount applied to it using woocommerce dynamic pricing plugin role based discounts. There may be an easier way to do it but I haven't found it as yet but this code worked for me.

    Full code here: http://pastebin.com/wf3aZbs3

  4. bcworkz
    Member
    Posted 1 year ago #

    Glad that worked for you. I'm rarely so good at getting array references right on first try.

    On further thought, a friendly array only has appeal to us mere humans, the code is better off the way things are, as it stops looking once it finds what it's looking for. No point building an entire array each time for a single user. For the amount of data involved, the difference in execution time would be infinitesimal either way.

    But thanks for posting all the info.

  5. sysins
    Member
    Posted 1 year ago #

    Makes sense, thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic