Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Assumption </h2> <p>I believe your <code>cache</code> is always in the format below </p> <p><strong>Cache Format:</strong></p> <pre><code>$cache = array( 0 =&gt; (object) (array( 'currency' =&gt; 1, 'role' =&gt; 3, 'client_company' =&gt; '', 'client_group' =&gt; '', 'hourly_rate' =&gt; '115.00' )), 1 =&gt; (object) (array( 'currency' =&gt; 1, 'role' =&gt; 1, 'client_company' =&gt; '', 'client_group' =&gt; '', 'hourly_rate' =&gt; '115.00' )), 2 =&gt; (object) (array( 'currency' =&gt; 1, 'role' =&gt; 3, 'client_company' =&gt; 58, 'client_group' =&gt; '', 'hourly_rate' =&gt; '110.00' )) ); </code></pre> <h2>Your Revised Function </h2> <pre><code>$param = array( "role" =&gt; 1, "currency" =&gt; 1 ); echo find($cache, $param)-&gt;hourly_rate; </code></pre> <p><strong>Function Used</strong></p> <pre><code>function find($cache, $param) { $mx = array(); if (! isset($param['role']) || ! isset($param['currency'])) throw new Exception("Missing Role Or Currency"); foreach ( $cache as $k =&gt; $r ) { foreach ( array_keys(array_intersect($param, (array) $r)) as $key ) { if ($r-&gt;{$key} == $param[$key]) { isset($mx[$k]) ? $mx[$k] ++ : $mx[$k] = 1; } } } arsort($mx); return $cache[key($mx)]; } </code></pre> <p><br /><br /> <br /><br /></p> <h2>More Complex: Another Approach </h2> <p><strong>Usage</strong> </p> <pre><code>$param = array( "role" =&gt; 1, "currency" =&gt; 1 ); $process = new Process($cache); echo $process-&gt;find($param)-&gt;best()-&gt;hourly_rate; // Outputs 115.00 </code></pre> <p><strong>Multiple Results</strong> </p> <p>When find best fit .. there is possibility you would get more than one result </p> <pre><code>$param = array( "role" =&gt; 3, "currency" =&gt; 1 ); $process = new Process($cache); var_dump($process-&gt;find($param)-&gt;results()); </code></pre> <p><strong>Output</strong> </p> <pre><code>array (size=2) 0 =&gt; object(stdClass)[1] public 'currency' =&gt; int 1 public 'role' =&gt; int 3 public 'client_company' =&gt; string '' (length=0) public 'client_group' =&gt; string '' (length=0) public 'hourly_rate' =&gt; string '115.00' (length=6) 2 =&gt; object(stdClass)[3] public 'currency' =&gt; int 1 public 'role' =&gt; int 3 public 'client_company' =&gt; int 58 public 'client_group' =&gt; string '' (length=0) public 'hourly_rate' =&gt; string '110.00' (length=6) </code></pre> <p><strong>Not getting best result</strong> </p> <p>You can see based on your parameters you are getting 2 if you are looking for cheapest prize and you call </p> <pre><code>$param = array( "role" =&gt; 3, "currency" =&gt; 1 ); echo Process::quick($cache, $param)-&gt;best()-&gt;hourly_rate; // returns 115.00 but that is not the cheapest </code></pre> <p><strong>Resolution</strong></p> <p>The solution is you can add filter and <code>sort</code> </p> <pre><code>$param = array( "role" =&gt; 3, "currency" =&gt; 1 ); $sort = function ($a, $b) { return $a-&gt;hourly_rate &lt; $b-&gt;hourly_rate ? - 1 : 1; }; echo Process::quick($cache, $param)-&gt;sort($sort)-&gt;best()-&gt;hourly_rate; // 110 </code></pre> <p><strong>Getting all Related</strong></p> <p>You can also just loop through all the result and select the columns you want insted of just getting best result </p> <pre><code>foreach ( Process::quick($cache, $param)-&gt;sort($sort)-&gt;getColoum("client_company", "hourly_rate") as $result ) { print_r($result); } </code></pre> <p><strong>Output</strong> </p> <pre><code>stdClass Object ( [client_company] =&gt; 58 [hourly_rate] =&gt; 110.00 ) stdClass Object ( [client_company] =&gt; [hourly_rate] =&gt; 115.00 ) </code></pre> <p><strong>Updated Class</strong></p> <p>To add all this additional functions you need to upgrade your class to</p> <pre><code> class Process implements JsonSerializable, IteratorAggregate { private $cache; private $matrix = array(); private $final = array(); function __construct($cache) { $this-&gt;cache = $cache; } function find($param) { if (! isset($param['role']) || ! isset($param['currency'])) throw new Exception("Missing Role Or Currency"); foreach ( $this-&gt;cache as $k =&gt; $rate ) { $keys = array_intersect($param, (array) $rate); foreach ( array_keys($keys) as $key ) { if ($rate-&gt;{$key} == $param[$key]) { isset($this-&gt;matrix[$k]) ? $this-&gt;matrix[$k] ++ : $this-&gt;matrix[$k] = 1; } } } arsort($this-&gt;matrix); $this-&gt;matrix = array_keys(array_filter($this-&gt;matrix, function ($v) { return $v &gt;= 2; })); $this-&gt;final = $this-&gt;sortArray($this-&gt;cache, $this-&gt;matrix); return $this; } public static function quick($cache, $param) { $process = new Process($cache); return $process-&gt;find($param); } public function best() { reset($this-&gt;final); return empty($this-&gt;final) ? : current($this-&gt;final); } public function results() { return $this-&gt;final; } public function limit($length = 0) { $this-&gt;final = array_slice($this-&gt;final, 0, $length); return $this; } public function sort(Callable $function) { usort($this-&gt;final, $function); return $this; } public function getColoum() { $arg = array_flip(func_get_args()); foreach ( $this-&gt;final as &amp;$s ) { foreach ( $s as $k =&gt; $v ) { if (! isset($arg[$k])) unset($s-&gt;{$k}); } } return $this; } public function getIterator() { return new ArrayIterator($this-&gt;final); } public function jsonSerialize() { return json_encode($this-&gt;final); } public function __toString() { return $this-&gt;jsonSerialize(); } private function sortArray(array $array, array $orderArray) { $ordered = array(); foreach ( $orderArray as $key =&gt; $value ) { array_key_exists($value, $array) and $ordered[$value] = $array[$value]; } return $ordered; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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