Re: [PHP] Finding next recored in a array
At 4:14 PM -0400 9/19/07, brian wrote: tedd wrote: At 11:52 AM -0400 9/17/07, brian wrote: tedd wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record What the next record? Try: $array = array('0','1','3','5','8','15','25'); $val = 5; echo($array[array_search($val, $array)+1]); Cheers, tedd Not quite: $array = array('0','1','3','5','8','15','25'); $val = 25; echo($array[array_search($val, $array)+1]); Notice: Undefined offset: 7 ... brian Duh? You program for that -- you want me to write the entire code? ~sigh~ Grasshopper, the point i was trying to make is that your example displays what *not* to do with array_search(). Though a wonderful teaching aid in itself, it falls somewhat short of being a reasonable solution for the OP. brian ~sigh~ Sorry to disappoint you master. But I was using array_search() to search an array -- it seemed like a reasonable thing to do. So why do you say it's an example of what *not* to do with array_search()? Please be specific. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
tedd wrote: At 4:14 PM -0400 9/19/07, brian wrote: tedd wrote: Duh? You program for that -- you want me to write the entire code? ~sigh~ Grasshopper, the point i was trying to make is that your example displays what *not* to do with array_search(). Though a wonderful teaching aid in itself, it falls somewhat short of being a reasonable solution for the OP. brian ~sigh~ Sorry to disappoint you master. But I was using array_search() to search an array -- it seemed like a reasonable thing to do. OK, so my joke left a bitter taste; sorry about that. What i'm trying to get across is that this list is here so that we may post our various problems in the hopes that someone else might see the solution we are missing. Sometimes, when we post what we *think* is the correct path to scripting Nirvana, another of us may point out a flaw in our logic. It happens to all of us. The responses to this list should not be taken to be either complete or fully-tested solutions. But, neither should potential bugs be ignored if noticed by any other reader. While your response certainly does *seem* to work just fine, it contains a bug that *will* bite at some point. So why do you say it's an example of what *not* to do with array_search()? Please be specific. Do you really expect me to write the entire code? Perhaps you could just meditate on this a little longer: Not quite: $array = array('0','1','3','5','8','15','25'); $val = 25; echo($array[array_search($val, $array)+1]); Notice: Undefined offset: 7 ... brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
At 11:52 AM -0400 9/17/07, brian wrote: tedd wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record What the next record? Try: $array = array('0','1','3','5','8','15','25'); $val = 5; echo($array[array_search($val, $array)+1]); Cheers, tedd Not quite: $array = array('0','1','3','5','8','15','25'); $val = 25; echo($array[array_search($val, $array)+1]); Notice: Undefined offset: 7 ... brian Duh? You program for that -- you want me to write the entire code? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
tedd wrote: At 11:52 AM -0400 9/17/07, brian wrote: tedd wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record What the next record? Try: $array = array('0','1','3','5','8','15','25'); $val = 5; echo($array[array_search($val, $array)+1]); Cheers, tedd Not quite: $array = array('0','1','3','5','8','15','25'); $val = 25; echo($array[array_search($val, $array)+1]); Notice: Undefined offset: 7 ... brian Duh? You program for that -- you want me to write the entire code? ~sigh~ Grasshopper, the point i was trying to make is that your example displays what *not* to do with array_search(). Though a wonderful teaching aid in itself, it falls somewhat short of being a reasonable solution for the OP. brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Rick Pasotto wrote: On Sun, Sep 16, 2007 at 06:04:45PM -0700, Richard Kurth wrote: Richard Kurth wrote: Rick Pasotto wrote: On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Nope. Just use array_search(). $k = array_search('5',$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I tried this and it gives me nothing back. It should give me a 8 $Campaign_array= array('0','1','3','5','8','15','25'); $val=5; $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I figured out way it was not working $k + 1 count needed to be $k + 1 count Yup. Sorry 'bout that. But now it works perfect if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } is very over-the-top. What's wrong with if (isset($Campaign_array[$k+1]) { echo $Campaign_array[$k + 1]; } Not to mention I don't really like the whole way this has been handled, I'm sure there are better ways, but for this we'd need to know more about what you're doing and what you're trying to achieve, which we obviously don't. - Tul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record What the next record? Try: $array = array('0','1','3','5','8','15','25'); $val = 5; echo($array[array_search($val, $array)+1]); Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
tedd wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record What the next record? Try: $array = array('0','1','3','5','8','15','25'); $val = 5; echo($array[array_search($val, $array)+1]); Cheers, tedd Not quite: $array = array('0','1','3','5','8','15','25'); $val = 25; echo($array[array_search($val, $array)+1]); Notice: Undefined offset: 7 ... brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Richard Kurth wrote: $query = SELECT day FROMemailcampaign where campaign_id = '$emailcampaign' AND member_id = '$members_id' ; $DB_Change_Campaign_Results = safe_query($query); while ( $row = mysql_fetch_array($DB_Change_Campaign_Results) ) { $Campaign_array[$row['day']] = $row; } # At this point you have arrays as values for your $Campaign_array # So, unless $k is an array that matches a sub array of $Campaign_array # you're never going to get a match $k = array_search($val,$Campaign_array); # What is $k at this point? An int (1, 2, 3, etc...) , string (Sunday, Monday, etc...) # Before I go any further I will need to know the above information. if ( ($k + 1) count($Campaign_array) ) { echo $Campaign_array[$k + 1]; } -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
M. Sokolewicz wrote: Rick Pasotto wrote: On Sun, Sep 16, 2007 at 06:04:45PM -0700, Richard Kurth wrote: Richard Kurth wrote: Rick Pasotto wrote: On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Nope. Just use array_search(). $k = array_search('5',$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I tried this and it gives me nothing back. It should give me a 8 $Campaign_array= array('0','1','3','5','8','15','25'); $val=5; $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I figured out way it was not working $k + 1 count needed to be $k + 1 count Yup. Sorry 'bout that. But now it works perfect if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } is very over-the-top. What's wrong with if (isset($Campaign_array[$k+1]) { echo $Campaign_array[$k + 1]; } Not to mention I don't really like the whole way this has been handled, I'm sure there are better ways, but for this we'd need to know more about what you're doing and what you're trying to achieve, which we obviously don't. What I am trying to is get all the days from a table where email campaign = number. and then look at the last day that was sent and find it in the list and get the next day that follows that day. At this point the script below is not working. So if you have any Ideas that would be a better way of doing this please let me know. $query = SELECT day FROM emailcampaign where campaign_id = '$emailcampaign' AND member_id = '$members_id'; $DB_Change_Campaign_Results = safe_query($query); for ($i=0; $i mysql_num_rows($DB_Change_Campaign_Results); $i++) { $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results); $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; } $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)){ echo $Campaign_array[$k + 1]; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Richard Kurth wrote: What I am trying to is get all the days from a table where email campaign = number. and then look at the last day that was sent and find it in the list and get the next day that follows that day. At this point the script below is not working. So if you have any Ideas that would be a better way of doing this please let me know. $query = SELECT day FROM emailcampaign where campaign_id = '$emailcampaign' AND member_id = '$members_id'; $DB_Change_Campaign_Results = safe_query($query); for ($i=0; $i mysql_num_rows($DB_Change_Campaign_Results); $i++) { $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results); $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; } $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)){ echo $Campaign_array[$k + 1]; } You can simplify this greatly: $Campaign_array = Array(); $DB_Change_Campaign_Results = safe_query($query); while ($row = mysql_fetch_array($DB_Change_Campaign_Results)) { array_push($Campaign_array, $row['day']); } Because you're only selecting day from the table, your row data consists of only one column. You're repeating stuff unnecessarily by doing this: $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; IOW, for the row that contains '5', you'd end up with $Campaign_array[5] == an array that essentially just points to the value '5'. I also noticed that your dump shows that the days begin with 0. If these are calendar days you're adding a further layer of complexity. brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
brian wrote: Richard Kurth wrote: What I am trying to is get all the days from a table where email campaign = number. and then look at the last day that was sent and find it in the list and get the next day that follows that day. At this point the script below is not working. So if you have any Ideas that would be a better way of doing this please let me know. $query = SELECT day FROM emailcampaign where campaign_id = '$emailcampaign' AND member_id = '$members_id'; $DB_Change_Campaign_Results = safe_query($query); for ($i=0; $i mysql_num_rows($DB_Change_Campaign_Results); $i++) { $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results); $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; } $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)){ echo $Campaign_array[$k + 1]; } You can simplify this greatly: $Campaign_array = Array(); $DB_Change_Campaign_Results = safe_query($query); while ($row = mysql_fetch_array($DB_Change_Campaign_Results)) { array_push($Campaign_array, $row['day']); } Because you're only selecting day from the table, your row data consists of only one column. You're repeating stuff unnecessarily by doing this: $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; IOW, for the row that contains '5', you'd end up with $Campaign_array[5] == an array that essentially just points to the value '5'. I also noticed that your dump shows that the days begin with 0. If these are calendar days you're adding a further layer of complexity. 0 represents the email campain to send 0 mens send the one that gois out the day the campain starts. the next number in the list is the next day a email is sent out would represent 5 day after the starting day -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
REVISED Try this ?php $query = SELECT day FROMemailcampaign WHERE campaign_id = '$emailcampaign' AND member_id = '$members_id' ; $DB_Change_Campaign_Results = safe_query($query); ## ## NOTICE: changed from array to assoc ## $Campaign_array = array(); while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) { ## Switched to an indexed array with the day as the value instead of the key $Campaign_array[] = $row['day']; } # At this point you have arrays as values for your $Campaign_array # So, unless $k is an array that matches a sub array of $Campaign_array # you're never going to get a match $k = array_search($val,$Campaign_array); # What is $k at this point? An int (1, 2, 3, etc...) , string (Sunday, Monday, etc...) # Before I go any further I will need to know the above information. if ( isset($Campaign_array[($k + 1)])) { echo $Campaign_array[($k + 1)]; } else { echo 'Not found'; } -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Richard Kurth wrote: include (includes/location.php); $query = SELECTday FROMemailcampaign wherecampaign_id = '1' ANDmember_id = '8' ; $DB_Change_Campaign_Results = safe_query($query); $Campaign_array = array(); while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) { $Campaign_array[] = $row['day']; } if ( isset($Campaign_array[($k + 1)])) { echo $Campaign_array[($k + 1)]; } else { echo 'Not found'; } var_dump($Campaign_array); This is what I get now when I run this 1 *array* 0 = string '0' /(length=1)/ 1 = string '1' /(length=1)/ 2 = string '3' /(length=1)/ 3 = string '6' /(length=1)/ 4 = string '9' /(length=1)/ 5 = string '12' /(length=2)/ 6 = string '15' /(length=2)/ 7 = string '20' /(length=2)/ 8 = string '25' /(length=2)/ 9 = string '30' /(length=2)/ Are there going to be wholes in the date range? if so, you will have to do that last bit like this. ?php include (includes/location.php); # # Setting $k # Make sure that $k is an integer, not a string. # hence, no quotes $k = 5; $query = SELECT day FROMemailcampaign WHERE campaign_id = '1' AND member_id = '8' ; $DB_Change_Campaign_Results = safe_query($query); $Campaign_array = array(); while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) { $Campaign_array[] = $row['day']; } sort($Campaign_array); foreach ( $Campaign_array AS $day ) { if ( $day = $k ) { $day = next($Campaign_array); break; } } echo $day; var_dump($Campaign_array); ? -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Richard Kurth wrote: What I am trying to is get all the days from a table where email campaign = number. and then look at the last day that was sent and find it in the list and get the next day that follows that day. At this point the script below is not working. So if you have any Ideas that would be a better way of doing this please let me know. $query = SELECT day FROM emailcampaign where campaign_id = '$emailcampaign' AND member_id = '$members_id'; $DB_Change_Campaign_Results = safe_query($query); for ($i=0; $i mysql_num_rows($DB_Change_Campaign_Results); $i++) { $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results); $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; } $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)){ echo $Campaign_array[$k + 1]; } Ok, so just to recap (because I'm very confused now, even more after the many tries on the list since): You have table in your database called 'emailcampaign'. This table has a column called 'day'. Now, your table holds 1 row per campaign and the 'day' column signifies when the last mail has been sent. -- am I correct on this so far ? -- Next, you have an array which contains a FIXED collection of days on which an email should be sent, which looks like so: $days_to_send_email_on = array('0','1','3','5','8','15','25'); -- am I correct on this so far [2] ? -- You know the 'day' the last mail was sent, and you wish to check your FIXED array to find out when the next email should be sent. -- am I correct on this so far [3] ? -- If so, then there are a few things I'm wondering: 1. How did you come up with that fixed array? Is it the result of some kind of calculation? 1.1 If so, it'd be easier to solve it the mathematical way. 1.2 If #1 is a random result (ie. user-input, or different), then how about storing it in a table and doing: SELECT day WHERE day $last_day_it_was_sent_on ORDER BY day ASC LIMIT 1 2. Actually, that's all I'm wondering :) - Tul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Jim Lucas wrote: Richard Kurth wrote: $query = SELECTday FROMemailcampaign wherecampaign_id = '$emailcampaign' ANDmember_id = '$members_id' ; $DB_Change_Campaign_Results = safe_query($query); while ( $row = mysql_fetch_array($DB_Change_Campaign_Results) ) { $Campaign_array[$row['day']] = $row; } # At this point you have arrays as values for your $Campaign_array # So, unless $k is an array that matches a sub array of $Campaign_array # you're never going to get a match $k = array_search($val,$Campaign_array); # What is $k at this point? An int (1, 2, 3, etc...) , string (Sunday, Monday, etc...) # Before I go any further I will need to know the above information. if ( ($k + 1) count($Campaign_array) ) { echo $Campaign_array[$k + 1]; } This is what I get if I run the above script. From a var_dump($Campaign_array); I get *array* 0 = *array* 0 = string '0' /(length=1)/ 'day' = string '0' /(length=1)/ 1 = *array* 0 = string '1' /(length=1)/ 'day' = string '1' /(length=1)/ 3 = *array* 0 = string '3' /(length=1)/ 'day' = string '3' /(length=1)/ 6 = *array* 0 = string '6' /(length=1)/ 'day' = string '6' /(length=1)/ 9 = *array* 0 = string '9' /(length=1)/ 'day' = string '9' /(length=1)/ 12 = *array* 0 = string '12' /(length=2)/ 'day' = string '12' /(length=2)/ 15 = *array* 0 = string '15' /(length=2)/ 'day' = string '15' /(length=2)/ 20 = *array* 0 = string '20' /(length=2)/ 'day' = string '20' /(length=2)/ 25 = *array* 0 = string '25' /(length=2)/ 'day' = string '25' /(length=2)/ 30 = *array* 0 = string '30' /(length=2)/ 'day' = string '30' /(length=2)/ From a $val=5; $k = array_search($val,$Campaign_array); var_dump($k); I get boolean false -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Jim Lucas wrote: REVISED Try this ?php $query = SELECTday FROMemailcampaign WHEREcampaign_id = '$emailcampaign' ANDmember_id = '$members_id' ; $DB_Change_Campaign_Results = safe_query($query); ## ## NOTICE: changed from array to assoc ## $Campaign_array = array(); while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) { ## Switched to an indexed array with the day as the value instead of the key $Campaign_array[] = $row['day']; } # At this point you have arrays as values for your $Campaign_array # So, unless $k is an array that matches a sub array of $Campaign_array # you're never going to get a match $k = array_search($val,$Campaign_array); # What is $k at this point? An int (1, 2, 3, etc...) , string (Sunday, Monday, etc...) # Before I go any further I will need to know the above information. if ( isset($Campaign_array[($k + 1)])) { echo $Campaign_array[($k + 1)]; } else { echo 'Not found'; } include (includes/location.php); $query = SELECTday FROMemailcampaign wherecampaign_id = '1' ANDmember_id = '8' ; $DB_Change_Campaign_Results = safe_query($query); $Campaign_array = array(); while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) { $Campaign_array[] = $row['day']; } if ( isset($Campaign_array[($k + 1)])) { echo $Campaign_array[($k + 1)]; } else { echo 'Not found'; } var_dump($Campaign_array); This is what I get now when I run this 1 *array* 0 = string '0' /(length=1)/ 1 = string '1' /(length=1)/ 2 = string '3' /(length=1)/ 3 = string '6' /(length=1)/ 4 = string '9' /(length=1)/ 5 = string '12' /(length=2)/ 6 = string '15' /(length=2)/ 7 = string '20' /(length=2)/ 8 = string '25' /(length=2)/ 9 = string '30' /(length=2)/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Pass in the array and loop through it until you find the key, increment that, ensure that there is another value with that key, and return the key (or the value). (untested) function nextInArray($arr, $val) { $next_key = NULL; for ($i = 0; $i sizeof($arr);$i++) { if ($arr[$i] == $val) { $next_key = ++$i; break; } } // return the key: return (array_key_exists($next_key) ? $next_key : NULL); // or the value: return (array_key_exists($next_key) ? $arr[$next_key] : NULL); } However, in your example, you're searching for the key that points to the value '5'. What if the value '5' occurs more than once? brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
brian wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Pass in the array and loop through it until you find the key, increment that, ensure that there is another value with that key, and return the key (or the value). (untested) function nextInArray($arr, $val) { $next_key = NULL; for ($i = 0; $i sizeof($arr);$i++) { if ($arr[$i] == $val) { $next_key = ++$i; break; } } // return the key: return (array_key_exists($next_key) ? $next_key : NULL); // or the value: return (array_key_exists($next_key) ? $arr[$next_key] : NULL); } However, in your example, you're searching for the key that points to the value '5'. What if the value '5' occurs more than once? brian In my script the value of 5 will not reoccur the numbers are number of days from 0 up to 30 days. Thanks for the function -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Nope. Just use array_search(). $k = array_search('5',$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } Pass in the array and loop through it until you find the key, increment that, ensure that there is another value with that key, and return the key (or the value). (untested) function nextInArray($arr, $val) { $next_key = NULL; for ($i = 0; $i sizeof($arr);$i++) { if ($arr[$i] == $val) { $next_key = ++$i; break; } } // return the key: return (array_key_exists($next_key) ? $next_key : NULL); // or the value: return (array_key_exists($next_key) ? $arr[$next_key] : NULL); } However, in your example, you're searching for the key that points to the value '5'. What if the value '5' occurs more than once? From the docs: If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead. -- Now what liberty can there be where property is taken without consent?? -- Samuel Adams Rick Pasotto[EMAIL PROTECTED]http://www.niof.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Rick Pasotto wrote: On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Nope. Just use array_search(). $k = array_search('5',$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I tried this and it gives me nothing back. It should give me a 8 $Campaign_array= array('0','1','3','5','8','15','25'); $val=5; $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
Richard Kurth wrote: Rick Pasotto wrote: On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Nope. Just use array_search(). $k = array_search('5',$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I tried this and it gives me nothing back. It should give me a 8 $Campaign_array= array('0','1','3','5','8','15','25'); $val=5; $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I figured out way it was not working $k + 1 count needed to be $k + 1 count But now it works perfect -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding next recored in a array
On Sun, Sep 16, 2007 at 06:04:45PM -0700, Richard Kurth wrote: Richard Kurth wrote: Rick Pasotto wrote: On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote: Richard Kurth wrote: $Campaign_array| = array('0','1','3','5','8','15','25');| I know that I can find the next recored in a array using next. What I do not understand is if I know the last number was say 5 how do I tell the script that that is the current number so I can select the next record || I think you'll need your own function for this. Nope. Just use array_search(). $k = array_search('5',$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I tried this and it gives me nothing back. It should give me a 8 $Campaign_array= array('0','1','3','5','8','15','25'); $val=5; $k = array_search($val,$Campaign_array); if ($k + 1 count($Campaign_array)) { echo $Campaign_array[$k + 1]; } I figured out way it was not working $k + 1 count needed to be $k + 1 count Yup. Sorry 'bout that. But now it works perfect -- Our fatigue is often caused not by work, but by worry, frustration and resentment. -- Dale Carnegie Rick Pasotto[EMAIL PROTECTED]http://www.niof.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php