Jump to content

Recommended Posts

how do u check to see if somethink in a array twice please....

 

my example dosent work so how........

 

if there two names the same in a array i wont it...

 

<?php

$a=array("john","paul","john");

$name="john";

for($i=1; $i<3; $i++){

if(in_array($name,$a)){

echo " in array";

}else{

echo "not in array";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/128042-in-array-twice-help/
Share on other sites

my current array looks like this...........

 

Array
(
    [0] => 73.99.134.50

)
Array
(
    [0] => 37.939.163.50

)
Array
(
    [0] => 
)

 

my code

 

 

<?php

$filename = "downloads.log" ;

$dataFile = fopen( $filename, "r" ) ;

if ( $dataFile ){

while (!feof($dataFile)){

$buffer = fgets($dataFile, 4096);
       
$pattern="/([0-9\.]{0,4}[0-9\.]{0,4}[0-9\.]{0,4}[0-9]{0,4})/i";

preg_match_all($pattern,$buffer,$result[]);

$x=implode(' ',$result[0][0]);

$y=explode(' ',$buffer);

print_r($y);

$users_ip=$_SERVER['REMOTE_ADDR'];

$count = array_count_values($y);

if ($count[$users_ip] == 2) {

echo "<center>SORRY OUR RECORDS SHOW US THAT YOU HAVE ALREADY <br> 
DOWNLOADED YOUR FILE PLEASE CONTACT US!<center>";	

EXIT;
}
}

fclose($dataFile);

}else{

die( "fopen failed for $filename" ) ;
  }
?>

 

it wont find the two matches becouse how the array comes out any other idears mate.......

i can not use buffer[0][0]

 

currently the $x implode shows the correct results, but when i use explode you see from the array my results......

Link to comment
https://forums.phpfreaks.com/topic/128042-in-array-twice-help/#findComment-663048
Share on other sites

Another way to determine if there's a duplicate in an array

 

<?php
$array=array('a', 'b', 'c', 'a');
foreach($array as $var){
    if(count(array_keys($array, $var))>1){
        echo $var.' is in the array more than once<br>';
    } else{
        echo $var.' is a unique value in the array<br>';
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/128042-in-array-twice-help/#findComment-663104
Share on other sites

my current array looks like this...........

 

Array
(
    [0] => 73.99.134.50

)
Array
(
    [0] => 37.939.163.50

)
Array
(
    [0] => 
)

 

my code

 

 

<?php

$filename = "downloads.log" ;

$dataFile = fopen( $filename, "r" ) ;

if ( $dataFile ){

while (!feof($dataFile)){

$buffer = fgets($dataFile, 4096);
       
$pattern="/([0-9\.]{0,4}[0-9\.]{0,4}[0-9\.]{0,4}[0-9]{0,4})/i";

preg_match_all($pattern,$buffer,$result[]);

$x=implode(' ',$result[0][0]);

$y=explode(' ',$buffer);

print_r($y);

$users_ip=$_SERVER['REMOTE_ADDR'];

$count = array_count_values($y);

if ($count[$users_ip] == 2) {

echo "<center>SORRY OUR RECORDS SHOW US THAT YOU HAVE ALREADY <br> 
DOWNLOADED YOUR FILE PLEASE CONTACT US!<center>";	

EXIT;
}
}

fclose($dataFile);

}else{

die( "fopen failed for $filename" ) ;
  }
?>

 

it wont find the two matches becouse how the array comes out any other idears mate.......

i can not use buffer[0][0]

 

currently the $x implode shows the correct results, but when i use explode you see from the array my results......

Are you separating IP addresses by a space within your downloads.log? You'll be better of logging each separate IP on a new line. Then you can just use the following code:

$log_file = 'downloads.log';

// file() returns an array of lines
$ip_log = file($log_file);

// remove new lines
$ip_log = array_map('trim', $ip_log);

// check to see if the users ip is in the $ip_log array
if(in_array($_SERVER['REMOTE_ADDR'], $ip_log))
{
    echo 'Downloaded';
}
else
{
    echo 'Not downloaded';
}

Link to comment
https://forums.phpfreaks.com/topic/128042-in-array-twice-help/#findComment-663276
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.