473,394 Members | 1,828 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,394 software developers and data experts.

if in_array misfiring?

I want to add an value to an array if it is not already there.

There are four values.

Something is misfiring as it as the value to the array even if it is
already there.

The values are
$ml_ept, $scfmlk1d, $scfmlk2d and $scfmlk3d

This is the code I am using to try and add only values that are not
already in the array

$konept=$ml_ept;
$arr_epst = array($konept);
$konept=$scfmlk1d;
include("chk_epst_arr.php");
$konept=$scfmlk1d;
include("chk_epst_arr.php");
$konept=$scfmlk1d;
include("chk_epst_arr.php");
Breakdown of that code
// First Value
$konept=$ml_ept;

// Create array with first value
$arr_epst = array($konept);

// Second value assigned
$konept=$scfmlk1d;

// .... and processed to see if it is already in the array
include("chk_epst_arr.php");

// Third value assigned
$konept=$scfmlk1d;

// .... and processed to see if it is already in the array
include("chk_epst_arr.php");

// Fourth value assigned
$konept=$scfmlk1d;

// .... and processed to see if it is already in the array
include("chk_epst_arr.php");

The process file "chk_epst_arr.php" that checks to see if it exits
contains the following code.
if (in_array($konept, $arr_epst)) {
echo 'already there<br>';
} else {
$arr_epst[] = $konept;
}

Breakdown of that and what I think it should be doing

// use of "if" to see if the new value is already in the array
if (in_array($konept, $arr_epst)) {

// if "yes" echo "already there and line break
echo 'already there<br>';

// close if and begin else
} else {

// if "no" add the new value
$arr_epst[] = $konept;

// close the else
}

As said, no matter if the value is already in the array or not it is
adding all four values to the array.

What am I missing?

Any help greatly appreciated

Garry Jones
Sweden

Jul 13 '07 #1
3 1400
I had the same problem. The in_array() function can return FALSE when
the value you want to check has a value of NULL. Use the
array_key_exists() function instead.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Jul 13 '07 #2
The in_array() function reacts okay in other statements to null. But I
still can't get this working, I have tested array_key_exists() but it
gave me the same poor results. So, something else is wrong.

Any help appreaciated.

Garry Jones
Sweden

Jul 13 '07 #3
What happens if you eliminate the includes, that is, paste your code
where the includes would go?

-A
Jul 14 '07 #4

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

Similar topics

12
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...
2
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...
3
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...
6
by: Craig Keightley | last post by:
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; // A comma separated list of values...
5
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....
3
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);...
6
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) =...
3
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=...
3
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,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...

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.