finding the negative value in an array php mysql

Posted on 2008-10-18
Last Modified: 2013-12-13
$variables = array(  'cf_12_1_08', 'cf_11_1_08',  'cf_10_1_08', &&..  'cf_1_1_00',);

There is one negative INT in the cf_ array.
I want to look into this array and name a new variable equal to the negative value.

In the second scenario, there are multiple negative vaules in the array and I want to create a variable that is equal to the array. For e.g. $negative_values (-10000, -1000 ,  ...  , -1000)

Thanks for the help,

Question by:derekstattin
  • 4
  • 3

Expert Comment

ID: 22749939
Well, for the data in the array you have provided, I'm not sure how you are calculating what is negative or not? The format of the data, for example 'cf_12_1_08', appears to be some sort of non-standard data format. What are you considering a negative value?

If the values of the array are just integers, then you can use something like the following to add the negative values to a new array:
$negativeVariables = array();
foreach ($variables as $key => $value) {
     if ($value < 0) {
          $negativeVariables[] = $value;

Open in new window


Author Comment

ID: 22749965
'cf_12_1_08', 'cf_11_1_08',  'cf_10_1_08'
These are the names of the variables. For each of these INT, in a MYSQL database. there is a  negative or positive whole number.
I will try your suggestion and relpy shortly

Accepted Solution

crush83 earned 500 total points
ID: 22750002
Well, if you want to preserve the key names of the negative values, you should do this:
$negativeVariables = array();
foreach ($variables as $key => $value) {
     if ($value < 0) {
          $negativeVariables[$key] = $value;

Open in new window

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!


Author Comment

ID: 22750143
I used the code you posted above. I made a mistake in saying the variables are INT. They are actually decimal(10,2)
For some reason the code echos the last value in the array 0.5, even when I change <0 to < -1
Any suggestions? Thanks again

Expert Comment

ID: 22750245
First off, negative values are anything less than 0, not -1. So, you would use if ($value < 0.0)

Second, my code has no echo statements. I need to see how you are implementing it.

Author Comment

ID: 22750284

else $mysqli = mysqli_connect();
$username = $_SESSION['loggedin'];
$months = array('12_1_08','11_1_08','10_1_08','9_1_08','8_1_08','7_1_08','6_1_08','5_1_08','4_1_08','3_1_08','2_1_08','1_1_08','12_1_07','11_1_07','10_1_07','9_1_07','8_1_07','7_1_07','6_1_07','5_1_07','4_1_07','3_1_07','2_1_07','1_1_07','12_1_06','11_1_06','10_1_06','9_1_06','8_1_06','7_1_06','6_1_06','5_1_06','4_1_06','3_1_06','2_1_06','1_1_06','12_1_05','11_1_05','10_1_05','9_1_05','8_1_05','7_1_05','6_1_05','5_1_05','4_1_05','3_1_05','2_1_05','1_1_05','12_1_04','11_1_04','10_1_04','9_1_04','8_1_04','7_1_04','6_1_04','5_1_04','4_1_04','3_1_04','2_1_04','1_1_04','12_1_03','11_1_03','10_1_03','9_1_03','8_1_03','7_1_03','6_1_03','5_1_03','4_1_03','3_1_03','2_1_03','1_1_03','12_1_02','11_1_02','10_1_02','9_1_02','8_1_02','7_1_02','6_1_02','5_1_02','4_1_02','3_1_02','2_1_02','1_1_02','12_1_01','11_1_01','10_1_01','9_1_01','8_1_01','7_1_01','6_1_01','5_1_01','4_1_01','3_1_01','2_1_01','1_1_01','12_1_00','11_1_00','10_1_00','9_1_00','8_1_00','7_1_00','6_1_00','5_1_00','4_1_00','3_1_00','2_1_00','1_1_00');
//gather the topics
$query = "SELECT *, DATE_FORMAT(topic_create_time,  '%b %e %Y at %r') aS fmt_topic_create_time FROM db ORDER BY topic_create_time DESC";
$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
if (mysqli_num_rows($result) < 1) {
	//there are no topics, so say so
	$html = "<p><em>No topics exist.</em></p>";
} else {
// I would like to show this as percentage as well
  echo $rate;
		$html ='
		<table cellpadding="4" cellspacing="1" border="1" border-color="black" style="font-size:10px" >
			 <th>Payment Period</th>
				// convert months array to string with <th> nodes, remove the _1_ [month]
				preg_replace("/_1_/", "_", implode("</th>\n<th>",$months)) 
				. '</th>
	 while ($row = mysqli_fetch_assoc($result)) {
		$topic_id = $row['topic_id'];
		$topic_title = stripslashes($row['topic_title']);
		$topic_create_time = $row['fmt_topic_create_time'];
		$topic_owner = stripslashes($row['topic_owner']);
        $filename = stripslashes($row['filename']);
		$topic_id = $row['topic_id'];
		$topic_title = stripslashes($row['topic_title']);
		$topic_create_time = $row['fmt_topic_create_time'];
		$topic_owner = stripslashes($row['topic_owner']);
        $filename = stripslashes($row['filename']);
		$INV_ID = stripslashes($row['INV_ID']);
		$INV_U_N = stripslashes($row['INV_U_N']);
		$MS_F_NAME = stripslashes($row['MS_F_NAME']);
		$MS_ID = stripslashes($row['MS_ID']);
		$CHECK = stripslashes($row['CHECK']);
		$INV_ID = stripslashes($row['INV_ID']);
		$MS_INV_ID = stripslashes($row['MS_INV_ID']);
		$ID_CONT = stripslashes($row['ID_CONT']);
		$CONT_DIST_1_1_08 = stripslashes($row['CONT_DIST_1_1_08']);
		$cf_12_1_08 = stripslashes($row['cf_12_1_08']);
		$cf_11_1_08 = stripslashes($row['cf_11_1_08']);
		$cf_10_1_08 = stripslashes($row['cf_10_1_08']);
		$cf_9_1_08 = stripslashes($row['cf_9_1_08']);
		$cf_8_1_08 = stripslashes($row['cf_8_1_08']);
		$cf_7_1_08 = stripslashes($row['cf_7_1_08']);
		$cf_6_1_08 = stripslashes($row['cf_6_1_08']);
		$cf_5_1_08 = stripslashes($row['cf_5_1_08']);
		$cf_4_1_08 = stripslashes($row['cf_4_1_08']);
		$cf_3_1_08 = stripslashes($row['cf_3_1_08']);
		$cf_2_1_08 = stripslashes($row['cf_2_1_08']);
		$cf_1_1_08 = stripslashes($row['cf_1_1_08']);
		$cf_12_1_07 = stripslashes($row['cf_12_1_07']);
		$cf_11_1_07 = stripslashes($row['cf_11_1_07']);
		$cf_10_1_07 = stripslashes($row['cf_10_1_07']);
		$cf_9_1_07 = stripslashes($row['cf_9_1_07']);
		$cf_8_1_07 = stripslashes($row['cf_8_1_07']);
		$cf_7_1_07 = stripslashes($row['cf_7_1_07']);
		$cf_6_1_07 = stripslashes($row['cf_6_1_07']);
		$cf_5_1_07 = stripslashes($row['cf_5_1_07']);
		$cf_4_1_07 = stripslashes($row['cf_4_1_07']);
		$cf_3_1_07 = stripslashes($row['cf_3_1_07']);
		$cf_2_1_07 = stripslashes($row['cf_2_1_07']);
		$cf_1_1_07 = stripslashes($row['cf_1_1_07']);
		$cf_12_1_06 = stripslashes($row['cf_12_1_06']);
		$cf_11_1_06 = stripslashes($row['cf_11_1_06']);
		$cf_10_1_06 = stripslashes($row['cf_10_1_06']);
		$cf_9_1_06 = stripslashes($row['cf_9_1_06']);
		$cf_8_1_06 = stripslashes($row['cf_8_1_06']);
		$cf_7_1_06 = stripslashes($row['cf_7_1_06']);
		$cf_6_1_06 = stripslashes($row['cf_6_1_06']);
		$cf_5_1_06 = stripslashes($row['cf_5_1_06']);
		$cf_4_1_06 = stripslashes($row['cf_4_1_06']);
		$cf_3_1_06 = stripslashes($row['cf_3_1_06']);
		$cf_2_1_06 = stripslashes($row['cf_2_1_06']);
		$cf_1_1_06 = stripslashes($row['cf_1_1_06']);
		$cf_12_1_05 = stripslashes($row['cf_12_1_05']);
		$cf_11_1_05 = stripslashes($row['cf_11_1_05']);
		$cf_10_1_05 = stripslashes($row['cf_10_1_05']);
		$cf_9_1_05 = stripslashes($row['cf_9_1_05']);
		$cf_8_1_05 = stripslashes($row['cf_8_1_05']);
		$cf_7_1_05 = stripslashes($row['cf_7_1_05']);
		$cf_6_1_05 = stripslashes($row['cf_6_1_05']);
		$cf_5_1_05 = stripslashes($row['cf_5_1_05']);
		$cf_4_1_05 = stripslashes($row['cf_4_1_05']);
		$cf_3_1_05 = stripslashes($row['cf_3_1_05']);
		$cf_2_1_05 = stripslashes($row['cf_2_1_05']);
		$cf_1_1_05 = stripslashes($row['cf_1_1_05']);
		$cf_12_1_04 = stripslashes($row['cf_12_1_04']);
		$cf_11_1_04 = stripslashes($row['cf_11_1_04']);
		$cf_10_1_04 = stripslashes($row['cf_10_1_04']);
		$cf_9_1_04 = stripslashes($row['cf_9_1_04']);
		$cf_8_1_04 = stripslashes($row['cf_8_1_04']);
		$cf_7_1_04 = stripslashes($row['cf_7_1_04']);
		$cf_6_1_04 = stripslashes($row['cf_6_1_04']);
		$cf_5_1_04 = stripslashes($row['cf_5_1_04']);
		$cf_4_1_04 = stripslashes($row['cf_4_1_04']);
		$cf_3_1_04 = stripslashes($row['cf_3_1_04']);
		$cf_2_1_04 = stripslashes($row['cf_2_1_04']);
		$cf_1_1_04 = stripslashes($row['cf_1_1_04']);
		$cf_12_1_03 = stripslashes($row['cf_12_1_03']);
		$cf_11_1_03 = stripslashes($row['cf_11_1_03']);
		$cf_10_1_03 = stripslashes($row['cf_10_1_03']);
		$cf_9_1_03 = stripslashes($row['cf_9_1_03']);
		$cf_8_1_03 = stripslashes($row['cf_8_1_03']);
		$cf_7_1_03 = stripslashes($row['cf_7_1_03']);
		$cf_6_1_03 = stripslashes($row['cf_6_1_03']);
		$cf_5_1_03 = stripslashes($row['cf_5_1_03']);
		$cf_4_1_03 = stripslashes($row['cf_4_1_03']);
		$cf_3_1_03 = stripslashes($row['cf_3_1_03']);
		$cf_2_1_03 = stripslashes($row['cf_2_1_03']);
		$cf_1_1_03 = stripslashes($row['cf_1_1_03']);
		$cf_12_1_02 = stripslashes($row['cf_12_1_02']);
		$cf_11_1_02 = stripslashes($row['cf_11_1_02']);
		$cf_10_1_02 = stripslashes($row['cf_10_1_02']);
		$cf_9_1_02 = stripslashes($row['cf_9_1_02']);
		$cf_8_1_02 = stripslashes($row['cf_8_1_02']);
		$cf_7_1_02 = stripslashes($row['cf_7_1_02']);
		$cf_6_1_02 = stripslashes($row['cf_6_1_02']);
		$cf_5_1_02 = stripslashes($row['cf_5_1_02']);
		$cf_4_1_02 = stripslashes($row['cf_4_1_02']);
		$cf_3_1_02 = stripslashes($row['cf_3_1_02']);
		$cf_2_1_02 = stripslashes($row['cf_2_1_02']);
		$cf_1_1_02 = stripslashes($row['cf_1_1_02']);
		$cf_12_1_01 = stripslashes($row['cf_12_1_01']);
		$cf_11_1_01 = stripslashes($row['cf_11_1_01']);
		$cf_10_1_01 = stripslashes($row['cf_10_1_01']);
		$cf_9_1_01 = stripslashes($row['cf_9_1_01']);
		$cf_8_1_01 = stripslashes($row['cf_8_1_01']);
		$cf_7_1_01 = stripslashes($row['cf_7_1_01']);
		$cf_6_1_01 = stripslashes($row['cf_6_1_01']);
		$cf_5_1_01 = stripslashes($row['cf_5_1_01']);
		$cf_4_1_01 = stripslashes($row['cf_4_1_01']);
		$cf_3_1_01 = stripslashes($row['cf_3_1_01']);
		$cf_2_1_01 = stripslashes($row['cf_2_1_01']);
		$cf_1_1_01 = stripslashes($row['cf_1_1_01']);
		$cf_12_1_00 = stripslashes($row['cf_12_1_00']);
		$cf_11_1_00 = stripslashes($row['cf_11_1_00']);
		$cf_10_1_00 = stripslashes($row['cf_10_1_00']);
		$cf_9_1_00 = stripslashes($row['cf_9_1_00']);
		$cf_8_1_00 = stripslashes($row['cf_8_1_00']);
		$cf_7_1_00 = stripslashes($row['cf_7_1_00']);
		$cf_6_1_00 = stripslashes($row['cf_6_1_00']);
		$cf_5_1_00 = stripslashes($row['cf_5_1_00']);
		$cf_4_1_00 = stripslashes($row['cf_4_1_00']);
		$cf_3_1_00 = stripslashes($row['cf_3_1_00']);
		$cf_2_1_00 = stripslashes($row['cf_2_1_00']);
		$cf_1_1_00 = stripslashes($row['cf_1_1_00']);
		$seq = array( $cf_12_1_08,
$variables = array(  'cf_12_1_08'=>1,
$negativeVariables = array();
foreach ($seq as $key => $value) {
     if ($value <= 0) {
          $negativeVariables[$key] = $value;
		//$period_to_solve = count($months); this needs to see the period associated with the date where the value of the row is negative and verify by equaling the hard code ID_CONT in the invdb database 
		// reset sum to 0 for each record??
		$period_to_solve = $row['ID_CONT'];
		//First sum all of the period there is info for!
		$html .= "
		foreach($months as $month){
			$html .="<td>".$row['cf_'.$month]."</td>\n";
		$html .="</tr>";
	$html .=	"</table>";
<meta http-equiv="content-Type" content="text/html; charset=iso-8859-1" /><meta name="keywords" content="Members Page">
<meta name="Description" content="Members Page">
<title>Member Page</title>
<link rel="stylesheet" type="text/css" media="screen" href="/images/style_032508.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/images/style_0325081.css" />
<div id="nav_top"><a href="/">Home</a> | <a href="/about">About Us</a> | <a href="/contact">Contact</a> | <a href="/site map">Site Map</a>| <a href="/login">Log Out</a></div>
<div id="bkg" style="width:auto;">
<div id="body" style="font-size:10px; ">
<p><span class="heading" style="width:600px; padding-top:20px;">View Your Internal Rate of Return</span></p>
<?php echo $html; ?></th>
<?php foreach($months as $key => $value) {  print ":$key =&gt; $value <br/>"; } ?> 
<?php foreach($variables as $key => $value) {  print ":$key =&gt; $value <br/>"; } ?> 
<?php foreach($seq as $key => $value) {  print ":$key =&gt; $value <br/>"; } ?>

Open in new window


Author Comment

ID: 22750288
here is the page
Thanks again for the help

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question