473,889 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
6 4053
"Craig Keightley" <cr***@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);

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

I have no results but i cannot see why

Jul 17 '05 #2
No, that doesn't work either.
Any other suggestions?

Thanks Any way

Craig
"Jon Kraft" <jo*@jonux.co.u k> wrote in message
news:Xn******** *************** ***@130.133.1.4 ...
"Craig Keightley" <cr***@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);

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

I have no results but i cannot see why

Jul 17 '05 #3
"Craig Keightley" <cr***@sitedesi gn.net> wrote:
"Jon Kraft" <jo*@jonux.co.u k> wrote:
"Craig Keightley" <cr***@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);


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
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.u k> wrote in message
news:Xn******** *************** ***@130.133.1.4 ...
"Craig Keightley" <cr***@sitedesi gn.net> wrote:
"Jon Kraft" <jo*@jonux.co.u k> wrote:
"Craig Keightley" <cr***@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);


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
"Jon Kraft" <jo*@jonux.co.u k> schrieb im Newsbeitrag
news:Xn******** *************** ***@130.133.1.4 ...
"Craig Keightley" <cr***@sitedesi gn.net> wrote:
"Jon Kraft" <jo*@jonux.co.u k> wrote:
"Craig Keightley" <cr***@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);


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

HTH
Markus
Jul 17 '05 #6
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.u k> schrieb im Newsbeitrag
news:Xn******** *************** ***@130.133.1.4 ...
"Craig Keightley" <cr***@sitedesi gn.net> wrote:
"Jon Kraft" <jo*@jonux.co.u k> wrote:
> "Craig Keightley" <cr***@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);

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

HTH
Markus

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
6021
by: AJ Z | last post by:
I am using in_array() to search for a value ("other"), in order to validate a form. If I pass $_POST as the array to search PHP says that it is an invalid datatype. It is an array and if I copy it to another variable (i.e. $list = $_POST;) it works fine. The same is also true of implode(). Do you believe this is intended or is it a bug? --
2
1992
by: Mountain Man | last post by:
Hi, The in_array function sometimes malfunctions on my computer yielding the error message shown below. The code shown below usually works, but sometimes doesn't. This even happens with the same instance of code. Different results on different times I load the same page. Can anyone explain what's going on here? Best wishes,
3
10706
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if (!in_array($obj, $result)) array_push($result, $obj); } }
5
2377
by: Phil Powell | last post by:
Here is my array. Plain and simple enumerative array with values being strings. So why does this fail??? print_r(in_array('album', array_keys($boolword))); This produces FALSE or NULL. But it's right there IN the array!! What
4
3238
by: j-marvin | last post by:
hi- i am adding and and or operators to a search engine i will have hooked up to my personal bookmarks on my website. i wanted to get rid of duplicate page_url when i chain the conditions together like tcl or delphi or sqlite returns: http://j_m_mc.tripod.com 12 http://www.dubaron.com 11
3
2280
by: Tom Barnes | last post by:
Check out this code: // Start Code ------------- function test_in_array($val) { $a = array('key' => $val); printf("in_array: %d, value:%s<BR>", in_array('key', $a), $a); } test_in_array(0); test_in_array(1);
6
3289
by: Berimor | last post by:
Hi, i've been always used the in_array() function to check availabylity of a value in array. Until today. Simple code: (do not run it tho :) ) <? function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); }
3
1798
by: rich | last post by:
I keep getting the error: Warning: in_array(): Wrong datatype for second argument on line 679 here is the code. I declare and fill the array $specprimA = array(); $specprimA= getspecprim($dresstypeid); $primsecA=getprimsec(); Then I do a compare using in Array. I even put in the extra step of making sure I have something in the array with the is sizeof statement.
3
1894
by: Sonnich | last post by:
The following code is an exact copy of my current code. The idea is to check 2 parts (of string arrays), and add those only once to a common array. I check for existance of an array in a string, as the string contain more data that just .... well, the part from the options array is there, then it is added to the folders array. The problem is the line: if(!in_array($folders, $options1)) which works in the first case, but not...
0
9962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9809
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11190
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10442
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9607
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4647
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3256
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.