PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Finding next recored in a array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Finding next recored in a array

Réponse
 
LinkBack Outils de la discussion
Vieux 16/09/2007, 22h24   #1
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Finding next recored in a array

$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
||
  Réponse avec citation
Vieux 17/09/2007, 00h09   #2
brian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 17/09/2007, 00h35   #3
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 17/09/2007, 01h21   #4
Rick Pasotto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 rick@niof.net http://www.niof.net
  Réponse avec citation
Vieux 17/09/2007, 01h50   #5
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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]; }
  Réponse avec citation
Vieux 17/09/2007, 02h04   #6
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 17/09/2007, 02h30   #7
Rick Pasotto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 rick@niof.net http://www.niof.net
  Réponse avec citation
Vieux 17/09/2007, 09h24   #8
M. Sokolewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h13.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20714 seconds with 16 queries