Problem with recursive function

hello everyone.
I’m trying to implement shannon-fano algoritm for a given string
I’ve figured out this function but i can’t fogure out why it doesn’t work how i espect. What i’m doing basicly is :
Define a String.Enter in an array the frequancy each character occurs in the string and sort it descending.Then i call my function to divide the array in 2 arrays which combined frequancy of every item is roughly equal.Then i repeat the same for the two strings but it loops infinitly.I put count(array) to stop the execution if the array has only one element but it isn’t working
here is my function

``````function divide_array(\$array)
{

if(count(\$array)>1)
{

\$mid=0;
for(\$i=0;\$i<count(\$array);\$i++)
{
\$mid=\$mid+\$array[\$i]['occurrences'];
}
\$mid=\$mid/2;
\$sum=0;
echo "\$mid<br>";
foreach(\$array as \$li)
{
if(\$mid<=1){break;}// if i delete this it will loop infinitely
if(\$sum<\$mid)
{
\$sum=\$sum+ \$li['occurrences'];
\$up[]=array(
'occurrences'=>\$li['occurrences'],
'char'=>\$li['char'],);
\$array['atribute']="0";
\$key=array_search(\$li,\$array);

}
else
{
\$output = array_slice(\$array, \$key);

echo "<font color=red>UP</font>";
echo "<pre>";
print_r(\$up);
echo "</pre>";
echo "<font color=red>DOWN</font>";

echo "<pre>";
print_r(\$output);
echo "</pre>";
echo "<font size=29>UPC".count(\$up)."</font><br>";
divide_array(\$up);
echo "<font size=29>DWC".count(\$array)."</font><br>";
divide_array(\$output);
break;

}
}
``````

I think I’ll have a play with this.

``````
print_r(
sf_weight('AnthonySterling')
);

/*
Array
(
[n] => Array
(
[occurrences] => 3
[probability] => 0.79850769621777
)

[t] => Array
(
[occurrences] => 2
[probability] => 1.2039728043259
)

[r] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[l] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[g] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[e] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[i] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[y] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[h] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[o] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[a] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

[s] => Array
(
[occurrences] => 1
[probability] => 1.8971199848859
)

)
*/

``````
``````
function sf_sort(\$a, \$b){
if(\$a['occurrences'] === \$b['occurrences']){
return 0;
}
return \$a['occurrences'] < \$b['occurrences'] ? 1 : -1 ;
}

function sf_weight(\$string){
\$map = array();
foreach(str_split(\$string) as \$chr){
\$chr = strtolower(\$chr);
if(false === isset(\$map[\$chr])){
\$map[\$chr] = array(
'occurrences' => 0,
'probability' => 0
);
}
\$map[\$chr]['occurrences']++;
}
foreach(\$map as \$chr => &\$weight){
\$probability = \$weight['occurrences'] * (strlen(\$string) / 100);
\$weight['probability'] = log(1 / \$probability);
}
uasort(\$map, 'sf_sort');
return \$map;
}

``````

Next step, splitting the leaves into 2 equally weighted leaves.

Just a quick note, more to myself really. The following PHP array should be generated for ‘AAAAAAAAAAAAAAABBBBBBBCCCCCCDDDDDDEEEEE’ according to this Wiki article.

``````
#http://en.wikipedia.org/wiki/Shannon%E2%80%93Fano_coding
\$target = array(
0 => array(
0 => 'A',
1 => 'B',
),
1 => array(
0 => 'C',
1 => array(
0 => 'D',
1 => 'E'
)
),
);

``````