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 6 3975
"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
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
"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
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
"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
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
This discussion thread is closed Replies have been disabled for this discussion. Similar topics
12 posts
views
Thread by AJ Z |
last post: by
|
2 posts
views
Thread by Mountain Man |
last post: by
|
3 posts
views
Thread by Phil Powell |
last post: by
|
5 posts
views
Thread by Phil Powell |
last post: by
|
4 posts
views
Thread by j-marvin |
last post: by
|
3 posts
views
Thread by Tom Barnes |
last post: by
|
6 posts
views
Thread by Berimor |
last post: by
|
3 posts
views
Thread by rich |
last post: by
|
3 posts
views
Thread by Sonnich |
last post: by
| | | | | | | | | | |