in_array() problem

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

    in_array() problem

    I am trying to compare values of a string entered into an array but having
    no results, is this possible to achieve:


    <?php
    $ids = $row_rsProduct['itemList']; // A comma separated list of values
    (1,2,3,4,5,6,7, 8)
    $list = array($ids);
    if (in_array(1, $list)) {
    echo "In List";
    }
    ?>

    I have no results but i cannot see why

    Any help would be grateful

    Craig


  • Jon Kraft

    #2
    Re: in_array() problem

    "Craig Keightley" <craig@sitedesi gn.net> wrote:
    [color=blue]
    > I am trying to compare values of a string entered into an array but
    > having no results, is this possible to achieve:
    >
    >
    > <?php
    > $ids = $row_rsProduct['itemList']; // A comma separated list of
    > values
    > (1,2,3,4,5,6,7, 8)
    > $list = array($ids);[/color]

    Try this instead:
    $list = explode(",", $ids);

    HTH;
    JOn
    [color=blue]
    > if (in_array(1, $list)) {
    > echo "In List";
    > }
    > ?>
    >
    > I have no results but i cannot see why[/color]

    Comment

    • Craig Keightley

      #3
      Re: in_array() problem

      No, that doesn't work either.
      Any other suggestions?

      Thanks Any way

      Craig
      "Jon Kraft" <jon@jonux.co.u k> wrote in message
      news:Xns94BCA61 2EFDA6jonjonuxc ouk@130.133.1.4 ...[color=blue]
      > "Craig Keightley" <craig@sitedesi gn.net> wrote:
      >[color=green]
      > > I am trying to compare values of a string entered into an array but
      > > having no results, is this possible to achieve:
      > >
      > >
      > > <?php
      > > $ids = $row_rsProduct['itemList']; // A comma separated list of
      > > values
      > > (1,2,3,4,5,6,7, 8)
      > > $list = array($ids);[/color]
      >
      > Try this instead:
      > $list = explode(",", $ids);
      >
      > HTH;
      > JOn
      >[color=green]
      > > if (in_array(1, $list)) {
      > > echo "In List";
      > > }
      > > ?>
      > >
      > > I have no results but i cannot see why[/color][/color]


      Comment

      • Jon Kraft

        #4
        Re: in_array() problem

        "Craig Keightley" <craig@sitedesi gn.net> wrote:
        [color=blue]
        > "Jon Kraft" <jon@jonux.co.u k> wrote:[color=green]
        >> "Craig Keightley" <craig@sitedesi gn.net> wrote:
        >>[color=darkred]
        >> > I am trying to compare values of a string entered into an array but
        >> > having no results, is this possible to achieve:
        >> >
        >> >
        >> > <?php
        >> > $ids = $row_rsProduct['itemList']; // A comma separated list of
        >> > values
        >> > (1,2,3,4,5,6,7, 8)
        >> > $list = array($ids);[/color]
        >>
        >> Try this instead:
        >> $list = explode(",", $ids);[/color]
        >
        > No, that doesn't work either.
        > Any other suggestions?[/color]

        Have you echoed $ids? Is it really a comma separated list? What is the
        output?

        JOn

        Comment

        • Craig Keightley

          #5
          Re: in_array() problem

          I tried echoing the output and it works
          Does it matter that the field being called out from the database is a in
          text format?
          The text field is a comma separated list e.g. column a = 1,3,2,5,6

          "Jon Kraft" <jon@jonux.co.u k> wrote in message
          news:Xns94BCA9D 229256jonjonuxc ouk@130.133.1.4 ...[color=blue]
          > "Craig Keightley" <craig@sitedesi gn.net> wrote:
          >[color=green]
          > > "Jon Kraft" <jon@jonux.co.u k> wrote:[color=darkred]
          > >> "Craig Keightley" <craig@sitedesi gn.net> wrote:
          > >>
          > >> > I am trying to compare values of a string entered into an array but
          > >> > having no results, is this possible to achieve:
          > >> >
          > >> >
          > >> > <?php
          > >> > $ids = $row_rsProduct['itemList']; // A comma separated list of
          > >> > values
          > >> > (1,2,3,4,5,6,7, 8)
          > >> > $list = array($ids);
          > >>
          > >> Try this instead:
          > >> $list = explode(",", $ids);[/color]
          > >
          > > No, that doesn't work either.
          > > Any other suggestions?[/color]
          >
          > Have you echoed $ids? Is it really a comma separated list? What is the
          > output?
          >
          > JOn[/color]


          Comment

          • Markus Ernst

            #6
            Re: in_array() problem

            "Jon Kraft" <jon@jonux.co.u k> schrieb im Newsbeitrag
            news:Xns94BCA9D 229256jonjonuxc ouk@130.133.1.4 ...[color=blue]
            > "Craig Keightley" <craig@sitedesi gn.net> wrote:
            >[color=green]
            > > "Jon Kraft" <jon@jonux.co.u k> wrote:[color=darkred]
            > >> "Craig Keightley" <craig@sitedesi gn.net> wrote:
            > >>
            > >> > I am trying to compare values of a string entered into an array but
            > >> > having no results, is this possible to achieve:
            > >> >
            > >> >
            > >> > <?php
            > >> > $ids = $row_rsProduct['itemList']; // A comma separated list of
            > >> > values
            > >> > (1,2,3,4,5,6,7, 8)
            > >> > $list = array($ids);
            > >>
            > >> Try this instead:
            > >> $list = explode(",", $ids);[/color]
            > >
            > > No, that doesn't work either.
            > > Any other suggestions?[/color]
            >
            > Have you echoed $ids? Is it really a comma separated list? What is the
            > output?[/color]

            Maybe it is a string/integer problem? If the list comes out of a database it
            would likely be a string, so it is to be translated as:

            $list = array("1,2,3,4, 5,6,7,8");

            which produces an index array with one value ([0] => "1,2,3,4,5,6,7, 8").

            With the explode solution you produce an array such as: ([0] => "1", [1] =>
            "2" ....). This should work if 1="1" is true; I think that should be the
            case in PHP, but to be sure you could convert the needle argument of
            in_array to a string:

            $list = explode(",", $ids);

            if (in_array("1", $list)) {
            echo "In List";
            }

            or:
            if (in_array(strva l(1), $list)) {
            echo "In List";
            }

            HTH
            Markus


            Comment

            • Craig Keightley

              #7
              Re: in_array() problem

              Excellent
              The use of strval has worked a treat

              many thanks

              Craig

              "Markus Ernst" <derernst@NO#SP #AMgmx.ch> wrote in message
              news:406a7fab$0 $4097$afc38c87@ news.easynet.ch ...[color=blue]
              > "Jon Kraft" <jon@jonux.co.u k> schrieb im Newsbeitrag
              > news:Xns94BCA9D 229256jonjonuxc ouk@130.133.1.4 ...[color=green]
              > > "Craig Keightley" <craig@sitedesi gn.net> wrote:
              > >[color=darkred]
              > > > "Jon Kraft" <jon@jonux.co.u k> wrote:
              > > >> "Craig Keightley" <craig@sitedesi gn.net> wrote:
              > > >>
              > > >> > I am trying to compare values of a string entered into an array but
              > > >> > having no results, is this possible to achieve:
              > > >> >
              > > >> >
              > > >> > <?php
              > > >> > $ids = $row_rsProduct['itemList']; // A comma separated list[/color][/color][/color]
              of[color=blue][color=green][color=darkred]
              > > >> > values
              > > >> > (1,2,3,4,5,6,7, 8)
              > > >> > $list = array($ids);
              > > >>
              > > >> Try this instead:
              > > >> $list = explode(",", $ids);
              > > >
              > > > No, that doesn't work either.
              > > > Any other suggestions?[/color]
              > >
              > > Have you echoed $ids? Is it really a comma separated list? What is the
              > > output?[/color]
              >
              > Maybe it is a string/integer problem? If the list comes out of a database[/color]
              it[color=blue]
              > would likely be a string, so it is to be translated as:
              >
              > $list = array("1,2,3,4, 5,6,7,8");
              >
              > which produces an index array with one value ([0] => "1,2,3,4,5,6,7, 8").
              >
              > With the explode solution you produce an array such as: ([0] => "1", [1][/color]
              =>[color=blue]
              > "2" ....). This should work if 1="1" is true; I think that should be the
              > case in PHP, but to be sure you could convert the needle argument of
              > in_array to a string:
              >
              > $list = explode(",", $ids);
              >
              > if (in_array("1", $list)) {
              > echo "In List";
              > }
              >
              > or:
              > if (in_array(strva l(1), $list)) {
              > echo "In List";
              > }
              >
              > HTH
              > Markus
              >
              >[/color]


              Comment

              Working...