in_array() problem – PHP

  php

Q(Question):

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

A(Answer):

"Craig Keightley" <cr***@sitedesign.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);

HTH;
JOn
if (in_array(1, $list)) {
echo "In List";
}
?>

I have no results but i cannot see why

A(Answer):

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

Thanks Any way

Craig
"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4…

"Craig Keightley" <cr***@sitedesign.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);

HTH;
JOn

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

I have no results but i cannot see why

A(Answer):

"Craig Keightley" <cr***@sitedesign.net> wrote:

"Jon Kraft" <jo*@jonux.co.uk> wrote:

"Craig Keightley" <cr***@sitedesign.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);

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

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

JOn

A(Answer):

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" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4…

"Craig Keightley" <cr***@sitedesign.net> wrote:

"Jon Kraft" <jo*@jonux.co.uk> wrote:

"Craig Keightley" <cr***@sitedesign.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);

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

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

JOn

A(Answer):

"Jon Kraft" <jo*@jonux.co.uk> schrieb im Newsbeitrag
news:Xn**************************@130.133.1.4…

"Craig Keightley" <cr***@sitedesign.net> wrote:

"Jon Kraft" <jo*@jonux.co.uk> wrote:

"Craig Keightley" <cr***@sitedesign.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);

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

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

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(strval(1), $list)) {
echo "In List";
}

HTH
Markus

A(Answer):

Excellent
The use of strval has worked a treat

many thanks

Craig

"Markus Ernst" <derernst@NO#SP#AMgmx.ch> wrote in message
news:40**********************@news.easynet.ch…

"Jon Kraft" <jo*@jonux.co.uk> schrieb im Newsbeitrag
news:Xn**************************@130.133.1.4…

"Craig Keightley" <cr***@sitedesign.net> wrote:

"Jon Kraft" <jo*@jonux.co.uk> wrote:
> "Craig Keightley" <cr***@sitedesign.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);

No, that doesn’t work either.
Any other suggestions?
Have you echoed $ids? Is it really a comma separated list? What is the
output?

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(strval(1), $list)) {
echo "In List";
}

HTH
Markus

LEAVE A COMMENT