I have simple code:
foreach($value as $key){
echo"Data: $key";
}

I want what $key can't duplicate. If $key duplicate show message "Can't make action". If no duplicates print all "$key". Thanks for help

    No offense, but I'll assume that English is not your first language. However, I think maybe you are saying you want to avoid duplicate values in your $value array.

    Just use something like:

    if (in_array($key, $value)) echo "$key has already been submitted!";
    

      if script are:
      foreach($value as $subkey => $key){
      echo"Data: $key";
      }

      And key can't duplicate. how check? thanks for answer

        Thats when you make a temp Array

        foreach($value as $subkey => $key)
        {
            if (!in_array($temp,$key))
            {
                echo"Data: $key";
                $temp[]=$key;
            }
            else
            {
                echo "$key has already been submitted!";
            }
        } 
        

          If you are wanting to remove any duplicate values from an existing array, use array_unique .

            Print error: Warning: in_array(): Wrong datatype for second argument in blavla/

              Originally posted by Forever_xxl
              Print error: Warning: in_array(): Wrong datatype for second argument in blavla/

              [man]in_array[/man]
              bool in_array ( mixed needle, array haystack [, bool strict] )

              Searches haystack for needle and returns TRUE if it is found in the array, FALSE otherwise.

              So $temp and $key were accidentally written the wrong way around. You're a programmer, you should be able to work these things out.

                if (!in_array($temp,$key))

                it's not working, i exchange place now look: if (!in_array($key,$temp))

                script working vey well, but i don't know how change that don't print this error. This error can be hidden 😃

                  [man]in_array[/man] sorry it didnt work straight away however i didnt exactly test it. Also you will probably find after reading this you will still get an error im sure you will figure this one out also.

                    NOT POSSIBLE to check or $key was submitted or no? Nothing know how check it. Very need help. 🙁

                      Originally posted by Weedpacket
                      So $temp and $key were accidentally written the wrong way around. You're a programmer, you should be able to work these things out.

                        Write a Reply...