<p>This is the <a href="https://en.wikipedia.org/wiki/Subset_sum_problem" rel="nofollow noreferrer">Subset Sum Problem</a> and it is <a href="https://en.wikipedia.org/wiki/NP-complete" rel="nofollow noreferrer">NP-Complete</a>.</p>

<blockquote>
  <p>... given a set of integers and an integer s, does any non-empty subset sum to s? ...</p>
</blockquote>

<p>Here:</p>

<ul>
<li><strong>$requiredDonation</strong> is the <strong>s</strong></li>
<li><strong>$userDonation</strong> is the <strong>set of integers</strong></li>
<li><strong>magicalFunction</strong> returns the subset of the <strong>set of integers</strong> that sum to <strong>s</strong>. (A non-false answer is the true of the <em>proper</em> NP-Complete question and the false is the false)</li>
</ul>

<p>The <a href="https://en.wikipedia.org/wiki/Subset_sum_problem" rel="nofollow noreferrer">wikipedia link</a> provides some solutions for subset sum problems and google search others.</p>

<ul>
<li><a href="https://arxiv.org/pdf/1507.02318.pdf" rel="nofollow noreferrer">Here</a> is one recomendation.</li>
<li><a href="https://github.com/burento/Subset-Sum-Problem/blob/master/subset.php" rel="nofollow noreferrer">Here</a> is PHP based solver.</li>
</ul>

<p>You may also wish to evaluate the requirements, as the problem may get out of hand if the number of donations to search is large. (This is a property of NP-Complete problems. They can take a long time to solve exactly).</p>
Need proper array calculation for this function
<p>Here is my requirement of algorithm to find donater id which sum of donation<br>
is equal to required donation  </p>

<pre><code>&lt;?php

function magicalFunction($donationArray , $expactedValue ){
    /*need some algorithm get key of elements from $donationArray which sum is   
    equal to $expactedValue  $*/
}

$userDonation = array(
    '1' =&gt; 100,
    '4' =&gt; 8064,
    '5' =&gt; 578,
    '6' =&gt; 752,
    '21' =&gt; 512,
    '121' =&gt; 660,
    '152' =&gt; 135,
    '199' =&gt; 1350
);

$requiredDonation = 886;
$selectedDonatee = magicalFunction( $userDonation , $requiredDonatee ); 
// return false

$requiredDonation = 1465;
$selectedDonatee = magicalFunction( $userDonation , $requiredDonatee ); 
// return array(5, 6, 152);

?&gt;
</code></pre>