By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,706 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,706 IT Pros & Developers. It's quick & easy.

in_array() problem

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"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

Jul 17 '05 #2

P: n/a
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

Jul 17 '05 #3

P: n/a
"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
Jul 17 '05 #4

P: n/a
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

Jul 17 '05 #5

P: n/a
"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
Jul 17 '05 #6

P: n/a
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

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.