if in_array misfiring?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GarryJones

    #1

    if in_array misfiring?

    I want to add an value to an array if it is not already there.

    There are four values.

    Something is misfiring as it as the value to the array even if it is
    already there.

    The values are
    $ml_ept, $scfmlk1d, $scfmlk2d and $scfmlk3d

    This is the code I am using to try and add only values that are not
    already in the array

    $konept=$ml_ept ;
    $arr_epst = array($konept);
    $konept=$scfmlk 1d;
    include("chk_ep st_arr.php");
    $konept=$scfmlk 1d;
    include("chk_ep st_arr.php");
    $konept=$scfmlk 1d;
    include("chk_ep st_arr.php");


    Breakdown of that code
    // First Value
    $konept=$ml_ept ;

    // Create array with first value
    $arr_epst = array($konept);

    // Second value assigned
    $konept=$scfmlk 1d;

    // .... and processed to see if it is already in the array
    include("chk_ep st_arr.php");

    // Third value assigned
    $konept=$scfmlk 1d;

    // .... and processed to see if it is already in the array
    include("chk_ep st_arr.php");

    // Fourth value assigned
    $konept=$scfmlk 1d;

    // .... and processed to see if it is already in the array
    include("chk_ep st_arr.php");

    The process file "chk_epst_arr.p hp" that checks to see if it exits
    contains the following code.


    if (in_array($kone pt, $arr_epst)) {
    echo 'already there<br>';
    } else {
    $arr_epst[] = $konept;
    }

    Breakdown of that and what I think it should be doing

    // use of "if" to see if the new value is already in the array
    if (in_array($kone pt, $arr_epst)) {

    // if "yes" echo "already there and line break
    echo 'already there<br>';

    // close if and begin else
    } else {

    // if "no" add the new value
    $arr_epst[] = $konept;

    // close the else
    }

    As said, no matter if the value is already in the array or not it is
    adding all four values to the array.

    What am I missing?

    Any help greatly appreciated

    Garry Jones
    Sweden

  • Willem Bogaerts

    #2
    Re: if in_array misfiring?

    I had the same problem. The in_array() function can return FALSE when
    the value you want to check has a value of NULL. Use the
    array_key_exist s() function instead.

    Best regards,
    --
    Willem Bogaerts

    Application smith
    Kratz B.V.

    Comment

    • GarryJones

      #3
      Re: if in_array misfiring?

      The in_array() function reacts okay in other statements to null. But I
      still can't get this working, I have tested array_key_exist s() but it
      gave me the same poor results. So, something else is wrong.

      Any help appreaciated.

      Garry Jones
      Sweden

      Comment

      • axlq

        #4
        Re: if in_array misfiring?

        What happens if you eliminate the includes, that is, paste your code
        where the includes would go?

        -A

        Comment

        Working...