For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2004 > in_array() problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author in_array() problem
Craig Keightley

2004-03-30, 11:32 am

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

2004-03-30, 11:32 am

"Craig Keightley" <craig@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

2004-03-30, 11:32 am

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

Thanks Any way

Craig
"Jon Kraft" <jon@jonux.co.uk> wrote in message
news:Xns94BCA612EFDA6jonjonuxcouk@130.133.1.4...[color=darkred]
> "Craig Keightley" <craig@sitedesign.net> wrote:
>
>
> Try this instead:
> $list = explode(",", $ids);
>
> HTH;
> JOn
>


Jon Kraft

2004-03-30, 11:32 am

"Craig Keightley" <craig@sitedesign.net> wrote:

> "Jon Kraft" <jon@jonux.co.uk> wrote:
>
> 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
Craig Keightley

2004-03-31, 4:39 am

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.uk> wrote in message
news:Xns94BCA9D229256jonjonuxcouk@130.133.1.4...
> "Craig Keightley" <craig@sitedesign.net> wrote:
>
>
> Have you echoed $ids? Is it really a comma separated list? What is the
> output?
>
> JOn



Markus Ernst

2004-03-31, 4:39 am

"Jon Kraft" <jon@jonux.co.uk> schrieb im Newsbeitrag
news:Xns94BCA9D229256jonjonuxcouk@130.133.1.4...
> "Craig Keightley" <craig@sitedesign.net> wrote:
>
>
> 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


Craig Keightley

2004-03-31, 6:31 am

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...
> "Jon Kraft" <jon@jonux.co.uk> schrieb im Newsbeitrag
> news:Xns94BCA9D229256jonjonuxcouk@130.133.1.4...
of[color=darkred]
>
> 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
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com