Find value in multidimensional array then use it to only print sibling values - PHP
<p>this is the furthest I've gone into looping through and outputting data from arrays. It may be scratching the surface to some but I'm still wrapping my head around the basic concepts of working with arrays.</p>

<p>I have an multidimensional array with two main sets, each one is nested 5 deep.</p>

<p>I've formatted a <code>var_dump();</code> of the arrays - so I can see more clearly what's going on. </p>

<pre><code>Array ( 
    'set_56aad4d86660c'     =&gt; Array ( // first foreach
        'conditions'            =&gt; Array ( // second foreach
            '1'                     =&gt; Array ( 
                'type'                  =&gt; apply_to 
                'args'                  =&gt; Array ( 
                    'applies_to'            =&gt; roles 
                    'roles'                 =&gt; Array ( 
                        '0'                     =&gt; administrator 
                    ) 
                ) 
            ) 
        ) 

        'rules' =&gt; Array ( 
            '1'     =&gt; Array ( 
                'from'      =&gt; 1 
                'to'        =&gt; 4 
                'type'      =&gt; fixed_price 
                'amount'    =&gt; 10 
            ) 
        ) 
    ) 
    'set_56aad4d867064'     =&gt; Array ( 
        'conditions'            =&gt; Array ( 
            '1'                     =&gt; Array ( 
                'type'                  =&gt; apply_to 
                'args'                  =&gt; Array ( 
                    'applies_to'            =&gt; roles 
                    'roles'                 =&gt; Array ( 
                        '0'                     =&gt; trader 
                    ) 
                ) 
            ) 
        ) 
        'rules' =&gt; Array ( 
            '1'     =&gt; Array ( 
                'from'      =&gt; 5 
                'to'        =&gt; 10 
                'type'      =&gt; fixed_price 
                'amount'    =&gt; 5 
            ) 
        ) 
    ) 
)
</code></pre>

<p>I'm not having an issue looping through and printing any of the values in the array, I've made sure I can print them all.</p>

<p><strong>Where I'm stuck</strong></p>

<p>What I'm trying to do is - when an array set has the role of administrator, only output the values in it's corresponding array. </p>

<p>i.e the data in the 'rules' array - 'from', 'to', 'amount'.</p>

<p>I can print the details from all sets but not based on condition (current user)</p>

<p><strong>Here's where I am so far</strong></p>

<pre><code>// Pull the data
global $post, $product;

// get the current user's details
$current_user = wp_get_current_user();

// Loop through main rule sets array
$array_rule_sets = get_post_meta($post-&gt;ID, '_pricing_rules', true);

// Check to see if there are rule sets
if ($array_rule_sets &amp;&amp; is_array($array_rule_sets) &amp;&amp; sizeof($array_rule_sets) &gt; 0) {

    // Loop through rule sets
    foreach ( $array_rule_sets as $rule_set ) {

        // Get conditions 
        $conditions = $rule_set['conditions'];

        // Loop through conditions
        foreach ( $conditions as $key =&gt; $condition ) {

            // Get Roles
            $roles = $condition['args']['roles'];

            // Loop through roles
            foreach ( $roles as $role ) {

                if ( in_array( $role, (array)$current_user-&gt;roles ) ) {
                    $role_rules[] = $key; // getting stuck here
                }

            }

            // Loop through rules array and print pricing table values based on key
            foreach ( $rule_set['rules'] as $key =&gt; $value ) {
                $tempstring .= $rule_set['rules'][$key]['from']."- ".$rule_set['rules'][$key]['to']."&lt;/td&gt;";
                $tempstring .= $rule_set['rules'][$key]['amount'];
            }  

            // Print results
            echo $tempstring;              
        }
    }
}
</code></pre>

<p>I went through a tutorial which is where a lot of this code came from. It was for a slightly different case but it had enough to start me off. </p>

<p>I spent the day re-building it bit by bit. I've commented where I get stuck.</p>

<p>I'd really appreciate if someone can understand what I'm trying to do and if possible explain to me where I'm going wrong and if it's possible to achieve the desired outcome.</p>

<p>Many thanks in advance</p>