473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

First value in array not found with array_search( )

Claus Mygind
571 512MB
I create an array like this
Expand|Select|Wrap|Line Numbers
  1. $dispArray = array ("FIRST","LAST","name","ID","DIRECTPHON","MOBILE","MRMRS","myKey");
  2.  
I search on the array like this:
Expand|Select|Wrap|Line Numbers
  1. if(array_search ( $key , $searchArray )){....}
  2.  
Using firephp, I verified that the $key value of "FIRST" was included in the row set I returned.

Also if I move FIRST into another position in the array the value is found in the $dispArray. But then the new value that is first in the array is not found.

Very puzzling. Any suggestion?
Apr 1 '11 #1

✓ answered by Claus Mygind

The error in my thinking (I guess) was, I thought the "if" statement returned true or false which of course I now know it does not. So this seems to have corrected the problem.
Expand|Select|Wrap|Line Numbers
  1. if(array_search ( $key , $dispArray ) === false )
  2.  
I can test for false as it will return false if not found but will return the position in the array if found.

which now begs the question, is there a way to test for "Not false" so as to avoid this odd construct.
Expand|Select|Wrap|Line Numbers
  1. if(array_search ( $key , $searchArray ) === false )
  2. {
  3.   //do nothing
  4. }else{
  5. ...the code I want to execute ....
  6. }
  7.  

2 2594
Claus Mygind
571 512MB
The error in my thinking (I guess) was, I thought the "if" statement returned true or false which of course I now know it does not. So this seems to have corrected the problem.
Expand|Select|Wrap|Line Numbers
  1. if(array_search ( $key , $dispArray ) === false )
  2.  
I can test for false as it will return false if not found but will return the position in the array if found.

which now begs the question, is there a way to test for "Not false" so as to avoid this odd construct.
Expand|Select|Wrap|Line Numbers
  1. if(array_search ( $key , $searchArray ) === false )
  2. {
  3.   //do nothing
  4. }else{
  5. ...the code I want to execute ....
  6. }
  7.  
Apr 1 '11 #2
This had me stumped for a bit too.
I found that a negation in the if statement worked to test for the opposite case - therefore

if (!($myvar===false)){
//code
}
Dec 1 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: captain_2010 | last post by:
hi, >From below table want only the first value of each item id. and how to get it, tried to use top n function in select statement it takes only the first record. itemid qty 1 10 1...
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
1
by: Harry | last post by:
Hi, Just seeing if anyone can help me with a problem I am having with drop down lists. - I have a asp:dropdownlist that gets its values from a database. - The user then selects a value then...
0
by: northshore | last post by:
Hello, I am creating a windows application database. I have a primary table 'Individuals' and a lookup table 'Prefixes.' In the Individuals table, I have a column 'PrefixID' that references...
2
by: wish | last post by:
my problem is after asort($array) then i want to print out the first value of the array? Like this array( => april =>free =>ghost =>key) I notice that the first index number is not a zero...and...
5
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD...
2
by: Arnold | last post by:
Hi there, In a textbox containing a string of ID numbers each separated by a comma, how can I keep only the first value, and then remove all of the other numbers and commas behind it? The first...
2
by: programmerboy | last post by:
This questions is pretty easy, but let me ask since I wasn't able to figure it out. why I am always getting the first value from DropDownList when I have ddlStates.SelectedItem.Value; ...
13
by: Moah Scout | last post by:
Hi all! This forum has been very usefully for the Access Beginners like me. I believe my problem can only be solved in this forum. I have two tables (tblCustomers and tblOrders)and one form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.