Jump to content

Recommended Posts

 

This code doesn't work:...

 

if (in_array('1', $tickets)){
		echo "Yes";
		}

 

 

var_dump($tickets) :

 

array
  0 => string '1808' (length=4)
  1 => string '8224' (length=4)
  2 => string '8513' (length=4)
  3 => string '8564' (length=4)
  4 => string '9727' (length=4)

 

 

 

This code works:

 

if (in_array('1808', $tickets)){
		echo "Yes";
		}

 

 

I just want to FIND the "1" NOT the whole "1808"........

Link to comment
https://forums.phpfreaks.com/topic/268508-trying-to-use-in_array-as-a-preg_match/
Share on other sites

if (isset ($tickets[1])) {
    // This works.
}

 

in_array () checks the values in the array, not the keys. To check the keys I recommend using isset ().

Considering 1801 is at the 0 key, I don't think OP is trying to use the key.

Here you go. This function will work very similar to in_array() but it will work on a partial string match.

function str_in_array($needle, $array, $caseSensitive=false)
{
    $needle = (string) $needle;
    foreach($array as $index => $haystack)
    {
        $haystack = (string) $haystack;
        if($caseSensitive) {
            if(stripos($haystack, $needle)!==false) { return $index; }
        } else {
            if(strpos($haystack, $needle)!==false) { return $index; }
        }
    }
    return false;
}

 

EDIT: Revised function to typecast values as strings.

Here you go. This function will work very similar to in_array() but it will work on a partial string match.

function str_in_array($needle, $array, $caseSensitive=false)
{
    $needle = (string) $needle;
    foreach($array as $index => $haystack)
    {
        $haystack = (string) $haystack;
        if($caseSensitive) {
            if(stripos($haystack, $needle)!==false) { return $index; }
        } else {
            if(strpos($haystack, $needle)!==false) { return $index; }
        }
    }
    return false;
}

 

EDIT: Revised function to typecast values as strings.

 

 

 

 

 

Hey, I used it but it doesn't seem to show.

 

Code:

 


if (str_in_array('1', $tickets)){
		echo "Yes I found a match";
		}

 

Var_dump of $tickets (getting from a while loop)

 

 

array
  0 => string '1265' (length=4)
  1 => string '1315' (length=4)
  2 => string '1979' (length=4)
  3 => string '2361' (length=4)
  4 => string '2621' (length=4)
  5 => string '3199' (length=4)
  6 => string '3214' (length=4)
  7 => string '3224' (length=4)
  8 => string '3671' (length=4)
  9 => string '3825' (length=4)
  10 => string '3846' (length=4)
  11 => string '4375' (length=4)
  12 => string '4782' (length=4)
  13 => string '5188' (length=4)
  14 => string '5643' (length=4)
  15 => string '5703' (length=4)
  16 => string '5762' (length=4)
  17 => string '6376' (length=4)
  18 => string '6517' (length=4)
  19 => string '6867' (length=4)
  20 => string '7487' (length=4)
  21 => string '7553' (length=4)
  22 => string '8670' (length=4)
  23 => string '9126' (length=4)
  24 => string '9757' (length=4)

 

Even though the first 0,1,2,3 have 1 it's still not showing "echo "Yes I found a match".

 

Hmm any ideas?

 

 

 

All I want to do is check each ARRAY value individually, It sucks balls that in_array doesn't support this... thought your function I thought would work so it might be on my end, dunno yet.

 

 

 

 

All I want to do is check each ARRAY value individually, It sucks balls that in_array doesn't support this... thought your function I thought would work so it might be on my end, dunno yet.

 

And why would you expect a function to do something it was not designed to do because YOU have a particular need? There are functions to do "key" things and then it's up to the programmer to put on his thinking cap and write some logical code.

 

As to my function, it works perfectly fine. I specifically stated that the function should work similar to in_array(). So, you should use it the same. The function will return the INDEX of the found value. That index could be a '0' which will resolve to FALSE if a loose comparison is done! If you do not do an IDENTICAL (comparison) and the returned index is 0 you will be running the wrong logic.

 

Your responses in this thread and another one today are showing a lack of attention to detail that is making it frustrating to even provide help to you.

 

if (str_in_array('1', $tickets) !== false)
{
     echo "Yes I found a match";
}

All I want to do is check each ARRAY value individually, It sucks balls that in_array doesn't support this... thought your function I thought would work so it might be on my end, dunno yet.

 

And why would you expect a function to do something it was not designed to do because YOU have a particular need? There are functions to do "key" things and then it's up to the programmer to put on his thinking cap and write some logical code.

 

As to my function, it works perfectly fine. I specifically stated that the function should work similar to in_array(). So, you should use it the same. The function will return the INDEX of the found value. That index could be a '0' which will resolve to FALSE if a loose comparison is done! If you do not do an IDENTICAL (comparison) and the returned index is 0 you will be running the wrong logic.

 

Your responses in this thread and another one today are showing a lack of attention to detail that is making it frustrating to even provide help to you.

 

if (str_in_array('1', $tickets) !== false)
{
     echo "Yes I found a match";
}

 

 

 

 

 

 

NICE!!

 

//*Match 4 numbers in the wrong order win 20% of the jackpot. (Odds: 1 in 417)
		if (!in_array($winning_number, $tickets[$dailycheck['user_id']]) AND 

		(str_in_array(''.$winning_number[0].'', $tickets) !== false) AND
		(str_in_array(''.$winning_number[1].'', $tickets) !== false) AND
		(str_in_array(''.$winning_number[2].'', $tickets) !== false) AND
		(str_in_array(''.$winning_number[3].'', $tickets) !== false)
		) {
$jackpot20 = true;
//other code sniped
}

 

Pretty much now, it will check each ticket number per user, per user.  This is to detect if a user selected all 4 numbers right, but in the incorrect ORDER, as in the !in_array.

 

Then I just do your beautiful function str_in_array to check if each value has all 4 numbers but not in the correct order (HENCE !in_array IF) ,

 

works beautiful, topic solved..

 

I love foreachs and each time you help, you might not really "Think you're helping" but each time, I do come closer and closer on learning foreaches, I mean look back 1year ago, I was asking questions where as NOW I know off the top of my head, i'll get there sooner or later...

 

Thanks, solved.

 

Maybe in a new php version they will add a parameter to the in_array function so we can search for str like your function does? Would be nice.  Doesn't matter now though since I know how to though eh? :P

 

 

I made a suggestion on the php5-6 wishlist, hope they consider :P

 

This is to detect if a user selected all 4 numbers right, but in the incorrect ORDER, as in the !in_array.

 

No, that will not work. Programming takes analytical thought. If you cannot or will not do that, then hire someone to write the code for you. Or, at least provide some context of what you are trying to achieve. Otherwise, you just piss off the people trying to help you because you come up with the solution you *think* you need and ask for that. But, then you end up getting that solution (which is incorrect) and just waste everyone's time.

 

If the winning number is '1212' and a user's pick is '1234' the process - you asked for - will find that the user matched ALL the numbers! And, if you had simply stated what you were trying to achieve a real solution could have been provided.

//Function to return a string ordered alphanumericaly
function orderByDigits($strValue)
{
    $arrValue = str_split($strValue);
    sort($arrValue);
    return implode('', $arrValue);
}

//Check for exact or partial matches
if(in_array($winning_number, $tickets))
{
    //One or more tickets matched EXACTLY the numbers AND positions
}
else
{
    //Convert winning number and tickets into ordered values
    $ordered_winning_number = orderByDigits($strValue);
    $ordered_tickets = array_map('orderByDigits', $tickets);

    //Check if any tickets matched all the digits
    if(in_array($ordered_winning_number, $ordered_tickets))
    {
        //One or more tickets matched all the numbers
    }
    else
    {
        //None of the tickets matched all the numbers
    }
}

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.