Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the best-fit object from an array of objects
    text
    copied!<p>Given an array in the following structure (although obviously with many more items in it):</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [currency] =&gt; 1 [role] =&gt; 3 [client_company] =&gt; [client_group] =&gt; [hourly_rate] =&gt; 115.00 ) [1] =&gt; stdClass Object ( [currency] =&gt; 1 [role] =&gt; 1 [client_company] =&gt; [client_group] =&gt; [hourly_rate] =&gt; 115.00 ) [2] =&gt; stdClass Object ( [currency] =&gt; 1 [role] =&gt; 3 [client_company] =&gt; 58 [client_group] =&gt; [hourly_rate] =&gt; 110.00 ) ) </code></pre> <p>I'm trying to create a function that will take four parameters:</p> <ul> <li>$role</li> <li>$currency</li> <li>$company [optional]</li> <li>$group [optional]</li> </ul> <p>("groups" are children of "companies": if a group is specified, a parent company will always also be specified) </p> <p>...and that will return the "hourly rate" value from the item that best fits those parameters, on the basis that:</p> <p>if $row, $currency, $company and $group are specified:</p> <ul> <li>find a rate that matches the role, currency, company and group.</li> <li>if there isn't one, find one that matches the role, currency and company</li> <li>if there isn't one, find one that matches the role and currency</li> <li>if there isn't one, return FALSE</li> </ul> <p>if just $row, $currency and $company are specified:</p> <ul> <li>find a rate that matches the role, currency and company</li> <li>if there isn't one, find one that matches the role and currency</li> <li>if there isn't one, return FALSE</li> </ul> <p>if just $row and $currency are specified:</p> <ul> <li>find a rate that matches the role and currency</li> <li>if there isn't one, return FALSE</li> </ul> <p>What I've got is below, and it works. However, it's ugly as sin. There must be a more elegant way than just bashing a load of if/else and loops together. However, it's Friday and I've had too much pizza for lunch and my brain has become ensludged with cheese.</p> <p>Can you help?</p> <pre><code>$hourly_rate = FALSE; if ( !empty($group) &amp;&amp; !empty($company) ) { foreach ( $rates_cache as $rate ) { if ( $rate-&gt;currency == $currency &amp;&amp; $rate-&gt;role == $role &amp;&amp; (int) $rate-&gt;client_company === (int) $company &amp;&amp; (int) $rate-&gt;client_group === (int) $group ) { $hourly_rate = $rate-&gt;hourly_rate; } } if ( empty($hourly_rate) ) { foreach ( $rates_cache as $rate ) { if ( $rate-&gt;currency == $currency &amp;&amp; $rate-&gt;role == $role &amp;&amp; (int) $rate-&gt;client_company === (int) $company ) { $hourly_rate = $rate-&gt;hourly_rate; } } } if ( empty($hourly_rate) ) { foreach ( $rates_cache as $rate ) { if ( $rate-&gt;currency == $currency &amp;&amp; $rate-&gt;role == $role ) { $hourly_rate = $rate-&gt;hourly_rate; } } } }else if ( !empty($company) ) { foreach ( $rates_cache as $rate ) { if ( $rate-&gt;currency == $currency &amp;&amp; $rate-&gt;role == $role &amp;&amp; (int) $rate-&gt;client_company === (int) $company ) { $hourly_rate = $rate-&gt;hourly_rate; } } if ( empty($hourly_rate) ) { foreach ( $rates_cache as $rate ) { if ( $rate-&gt;currency == $currency &amp;&amp; $rate-&gt;role == $role ) { $hourly_rate = $rate-&gt;hourly_rate; } } } }else{ foreach ( $rates_cache as $rate ) { if ( $rate-&gt;currency == $currency &amp;&amp; $rate-&gt;role == $role ) { $hourly_rate = $rate-&gt;hourly_rate; } } } return $hourly_rate; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload