473,804 Members | 2,615 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 4049
"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
6014
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
1989
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
10694
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
2374
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
3237
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
2276
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
3285
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
1793
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
1891
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
9706
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
10325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9140
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
7615
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
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
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
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.