|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
$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 || |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 rick@niof.net http://www.niof.net |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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]; } |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 rick@niof.net http://www.niof.net |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|