in_array() fails but it's in the array

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

    in_array() fails but it's in the array

    array_keys($boo lword) = Array ( [0] => 'album' [1] => 'keywords' [2]
    => 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
    'image_creation _start_date' [6] => 'image_creation _end_date' [7] =>
    'image_location _city' [8] => 'image_location _state' [9] =>
    'image_location _country' )
    Here is my array. Plain and simple enumerative array with values
    being strings.

    So why does this fail???

    [PHP]
    print_r(in_arra y('album', array_keys($boo lword)));
    [/PHP]

    This produces FALSE or NULL. But it's right there IN the array!! What
    on earth is going on? Is this a failure in the part of in_array() that
    I am unaware of?

    This is a major showstopper in my search code PHP script I'm writing
    for my application, please help someone!

    Thanx
    Phil
  • Rasmus

    #2
    Re: in_array() fails but it's in the array

    have you tried with the proper syntax?

    array_keys($boo lword) = Array ( [0] => 'album',
    [1] => 'keywords',
    [2] => 'persons',
    [3] => 'events',
    [4] => 'image_alt',
    [5] => 'image_creation _start_date',
    [6] => 'image_creation _end_date',
    [7] => 'image_location _city',
    [8] => 'image_location _state',
    [9] => 'image_location _country' );

    R

    --
    Due to VERY heavy spam reception (24,000+/week), I use a fake address.
    Please write me in this newsgroup if you want to get in contact with me

    NEVER support spammers or companies sending out spam
    NEVER use links sent to you in a spam mail
    NEVER unsubscribe a spam message
    NEVER reply to spam

    "Phil Powell" <soazine@erols. com> wrote in message
    news:1cdca2a7.0 404190718.11c22 3a7@posting.goo gle.com...[color=blue]
    >
    > array_keys($boo lword) = Array ( [0] => 'album' [1] => 'keywords' [2]
    > => 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
    > 'image_creation _start_date' [6] => 'image_creation _end_date' [7] =>
    > 'image_location _city' [8] => 'image_location _state' [9] =>
    > 'image_location _country' )
    >
    >
    > Here is my array. Plain and simple enumerative array with values
    > being strings.
    >
    > So why does this fail???
    >
    > [PHP]
    > print_r(in_arra y('album', array_keys($boo lword)));
    > [/PHP]
    >
    > This produces FALSE or NULL. But it's right there IN the array!! What
    > on earth is going on? Is this a failure in the part of in_array() that
    > I am unaware of?
    >
    > This is a major showstopper in my search code PHP script I'm writing
    > for my application, please help someone!
    >
    > Thanx
    > Phil[/color]


    Comment

    • Paul Delannoy

      #3
      Re: in_array() fails but it's in the array

      Phil Powell a écrit:[color=blue]
      >
      > array_keys($boo lword) = Array ( [0] => 'album' [1] => 'keywords' [2]
      > => 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
      > 'image_creation _start_date' [6] => 'image_creation _end_date' [7] =>
      > 'image_location _city' [8] => 'image_location _state' [9] =>
      > 'image_location _country' )
      >
      >
      > Here is my array. Plain and simple enumerative array with values
      > being strings.
      >
      > So why does this fail???
      >
      > [PHP]
      > print_r(in_arra y('album', array_keys($boo lword)));
      > [/PHP][/color]

      What is $boolword ? it's inconsistent here, just place the name of the
      array as second arg : print_r(in_arra y('album', array_keys));

      Comment

      • Jan Pieter Kunst

        #4
        Re: in_array() fails but it's in the array

        In article <1cdca2a7.04041 90718.11c223a7@ posting.google. com>,
        soazine@erols.c om (Phil Powell) wrote:
        [color=blue]
        >
        > array_keys($boo lword) = Array ( [0] => 'album' [1] => 'keywords' [2]
        > => 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
        > 'image_creation _start_date' [6] => 'image_creation _end_date' [7] =>
        > 'image_location _city' [8] => 'image_location _state' [9] =>
        > 'image_location _country' )
        >
        >
        > Here is my array. Plain and simple enumerative array with values
        > being strings.
        >
        > So why does this fail???
        >
        > [PHP]
        > print_r(in_arra y('album', array_keys($boo lword)));
        > [/PHP]
        >[/color]

        You probably mean something like this:

        $boolword = array('album', 'keywords', ...);

        var_dump(in_arr ay('album', $boolword));

        JP

        --
        Sorry, <devnull@cauce. org> is een "spam trap".
        E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

        Comment

        • Phil Powell

          #5
          Re: in_array() fails but it's in the array

          No, sorry I meant exactly this:

          Code:
          $html .= "<select name=\"boolword['album']\">\n";
          ....
          And to handle it I would parse $_POST out to get $boolword and you get
          this:

          Code:
          print_r($boolword);
          Results:

          Array ('album' => 'AND', 'location' => '', 'image_name' => 'OR',
          'whatever' => '')
          Sorry I wasn't more clear. $boolword will be an associative array
          whose keys are string values that are dynamically generated from the
          name of the HTML form dropdown element 'boolword[something]'; the
          value being a boolean word.

          Problem is this: If I try to reference $boolword[$elementName] where
          $elementName = 'album', it fails:

          Code:
          print_r($boolword[$elementName]);
          Results: NULL

          However, if I reference $boolword["'$elementName' "] where $elementName
          = 'album', it works perfectly:

          Code:
          print_r($boolword["'$elementName'"]);
          Results: AND

          This is what I'm trying to convey. The associative array $boolword
          produces an array where the keys seem to have embedded single quotes
          inside the keys themselves!! Is this a PHP bug?

          Phil

          Jan Pieter Kunst <devnull@cauce. org> wrote in message news:<devnull-3E6771.18170619 042004@news1.ne ws.xs4all.nl>.. .[color=blue]
          > In article <1cdca2a7.04041 90718.11c223a7@ posting.google. com>,
          > soazine@erols.c om (Phil Powell) wrote:
          >[color=green]
          > >
          > > array_keys($boo lword) = Array ( [0] => 'album' [1] => 'keywords' [2]
          > > => 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
          > > 'image_creation _start_date' [6] => 'image_creation _end_date' [7] =>
          > > 'image_location _city' [8] => 'image_location _state' [9] =>
          > > 'image_location _country' )
          > >
          > >
          > > Here is my array. Plain and simple enumerative array with values
          > > being strings.
          > >
          > > So why does this fail???
          > >
          > > [PHP]
          > > print_r(in_arra y('album', array_keys($boo lword)));
          > > [/PHP]
          > >[/color]
          >
          > You probably mean something like this:
          >
          > $boolword = array('album', 'keywords', ...);
          >
          > var_dump(in_arr ay('album', $boolword));
          >
          > JP[/color]

          Comment

          • Jan Pieter Kunst

            #6
            Re: in_array() fails but it's in the array

            In article <1cdca2a7.04042 10749.bd64d03@p osting.google.c om>,
            soazine@erols.c om (Phil Powell) wrote:
            [color=blue]
            >
            Code:
            > $html .= "<select name=\"boolword['album']\">\n";
            > ...
            >
            [/color]

            [...]
            [color=blue]
            > This is what I'm trying to convey. The associative array $boolword
            > produces an array where the keys seem to have embedded single quotes
            > inside the keys themselves!! Is this a PHP bug?
            >
            > Phil[/color]

            No, it's because you put the single quotes in the HTML. If you want to
            use [] syntax to get an array into $_REQUEST, you can either do this:

            <select name="boolword[]">

            for a numeric array $_REQUEST['boolword'], or this:

            <select name="boolword[album]">

            for an associative array $_REQUEST['boolword']. You shouldn't quote the
            array key in the HTML. Quoting array keys is only needed in PHP code.

            JP

            --
            Sorry, <devnull@cauce. org> is een "spam trap".
            E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

            Comment

            Working...