first i read file and i wana count numbers, i think i should store them in array and then delete repetitive numbers,every line have two number that separated by space,
be;ow is my code but i think for limit memory access its dosnt work,
I get that the array_unique was to delete duplicates, but I figured that if the problem is that you’re using too much memory, using an indexed array might help because instead of loading all the numbers (which might overflow memory) and then deleting duplicates, it would mean you will only create an array element for each unique number, as the array is being built.
The point of creating an indexed array is that you won’t have any duplicates. Because the index of the array is the number ($adad), each time it finds the same number it will either create the new array element, or overwrite the existing one.
but i received this error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in C:\xamp\htdocs\webgraf\counter.php on line 45
If you’re genuinely using more memory than you have available, then you’ll have to figure out a different way of solving the problem. Maybe what you need to do is create a database table (perhaps a temporary one), add a row containing each number, then you can count them either during the build or later with a query. You might be able to do something with an area of memory and set the appropriate bit to 1 - what’s the maximum value of any of the numbers, and are there any negative ones? Are there decimals, or just integers?
So does an indexed array still run out of memory?
<?php
echo "hello ,welcome to retieval page";
ini_set('max_execution_time', 360);
echo "<br>" ;
//$file = file('web-graph.txt');
ini_set('memory_limit', '2048M');
$sumlink=0; //tedade linkga
$sumpage = array();;
$file = fopen( "web-graph.txt", "r" ) or exit ( "Unable to open file!" ) ;
$str = fgets($file);
while ( !feof ( $file ) )
{
//shomaresh link
$sumlink++;
//shomaresh page
$tmp = explode(" ", $str);
$sumpage[$tmp[0]]=1;
$sumpage[$tmp[1]]=1;
$str = fgets($file);
}
echo "تعداد لینک ها =".$sumlink; // number of lines in file
echo "<br><br><br><br>";
$x=count($sumpage);
echo "تعداد صفحات=".$x;
fclose( $file ) ;
?>
with an plain array like the above in your original code, every time you create a new array element it gets a new element number starting from zero, and your number from the file is put into the contents of that array. So if you have a file that contained the number 105 over and over again, 13451 times, you’d see the above. You can see that takes up 13451 array elements (0-13450) which contain the same number. Using an indexed array, where it doesn’t really have an element number but uses something else instead, it removes the need to create duplicates. So for the same file, we’d get
$sumpage[105] = 1;
and no matter how many times your file contains the value 105, it will only ever create that one array element, and a separate one for each different number. You could enhance the code to count how many times each value appears, and the sample code I posted doesn’t check whether explodes returns 0, 1 or 2 elements and in live code you’d need to do that.
It’s still only good luck that this version happened to be under the memory limit - that would depend on the spread of numbers and how likely duplicates are. But I’m glad it helped.
with this i chechked before this index is valued or not, but no change,i mexed up with this,
big problem is when left number reapeat in right side for first time,the code assume its new unique number,and if left number appear more than one time in right side the assume just one unique number,
the code just can know unique numbers in same side,if its repeat in other side,code assume its new number,
See the comment in the other topic - you need to trim the $tmp elements to get rid of leading and trailing spaces. For an array index, “1” is not the same as "1 ".