/    Sign up×
Community /Pin to ProfileBookmark

Selecting position in the array

Hi all
I’m trying to figure out how can I get my next position in the array
Lets say I have array

red
green
blue
grey
orange

nowsay we have variable value “blue”

Next I want to do something to see that after blue we have grey and returan value of grey (basically get the value that comes after the variable)

Any ideas how to do that?

Tnx

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 01.2007 — [code=php]
$color = 'blue'
$ix = (array_search($color, $array))
{
if($ix !== FALSE)
{
if(array_key_exists($ix + 1, $array))
{
echo "Next after $color is: " . $array[$ix + 1];
}
else
{
echo "There is no color after $color.";
}
}
else
{
echo "There is no color $color in the array";
}
}
[/code]

Note: this assumes an array indexed with sequential numbers. If it is not, then you could probably do it with a foreach loop and the next() function.
Copy linkTweet thisAlerts:
@alexusauthorApr 01.2007 — ok thanx, let me see how it works!
Copy linkTweet thisAlerts:
@pcthugApr 01.2007 — Allows for both arrays indexed with sequential numbers and with defined keys:[code=php]
$color = 'blue';
if (in_array($color, $array))
{
while (current($array) != $color)
{
next($array);
}
}
else
{
echo "There is no color $color in the array";
}

echo "Next after $color is: " . next($array);[/code]
Copy linkTweet thisAlerts:
@alexusauthorApr 01.2007 — what does it mean indexed array?

my array is what i get from MySQL.
Copy linkTweet thisAlerts:
@pcthugApr 01.2007 — What function (mysql_fetch_assoc, mysql_fetch_array, etc.) are you using to [I]fetch[/I] your array from your MySQL Database?
Copy linkTweet thisAlerts:
@alexusauthorApr 01.2007 — mysql_fetch_array
Copy linkTweet thisAlerts:
@pcthugApr 01.2007 — and are you passing a second argument like (MYSQL_NUM, MYSQL_ASSOC, MYSQL_BOTH)?
Copy linkTweet thisAlerts:
@alexusauthorApr 01.2007 — here is how i do get info for the array, im think im lost now

[code=php]
$sql_next = "select id, page_name from tp_pages WHERE group_='GeneralPlanning'";
$result_next = mysql_query($sql_next);
$row_next = mysql_fetch_array($result_next)
[/code]
Copy linkTweet thisAlerts:
@pcthugApr 01.2007 — It's probably going to get really confusing if I try to explain what your current code is doing, I'd suggest using replacing your current code with the following: [code=php]
$sql_next = "select id, page_name from tp_pages WHERE group_='GeneralPlanning'";
$result_next = mysql_query($sql_next);
$row_next = mysql_fetch_array($result_next, MYSQL_NUM);[/code]

And now you may use either mine or NogDog's code to show the [i]next[/i] element in the array result.
Copy linkTweet thisAlerts:
@alexusauthorApr 01.2007 — well it seams that I got real confused now one way or another.

I'm getting something wrong here. the script gives me the same info as the curent row, and not the next row. also if my row has id, page_name im getting page name outputed insted of id, plus its is the page name of curent row id.

So say actuall data look like this:

45, Fin Data

46, Address

when I set curent row as 45, the next row outut says Fin data, instead of 46

[code=php]
$sql_next = "select id, page_name from tp_pages WHERE group_='GeneralPlanning'";
$result_next = mysql_query($sql_next);
$row_next = mysql_fetch_array($result_next, MYSQL_NUM);
///
$color = '45';
if (in_array($color, $row_next)){
while (current($row_next) != $color){
next($row_next);
}
}else{
echo "<br>There is no color $color in the array";
}

echo "<br>Next after $color is: <b>" . next($row_next) ."</b>";
[/code]
×

Success!

Help @alexus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...