473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Looking for a shortcut: Knowing the index position of an array, howcan i get the next $key available

SM
Hello,

Knowing the index position of an array, how can i get the next $key
available, if it exists. I've found a solution but I was wondering if
there was a shorcut in PHP.

i.e:

$cats = array (
456=>'myown',
186=>'getto',
714=>'many',
484=>'wondering',
775=>'ok'
);

In this example, I know that the index postion is 2. So the return
$key should be the next one available, if it exists... that is: 484.
This is how i coded:

$index = 2;
$id = array_slice($cats, $index, 1, true);

foreach($id as $key=>$value) {
echo $key;
}

I've look at all the functions on the php web site and couldn't find a
shortcut to this.

Any ideas?

Thanks in advance
Marco
Jun 2 '08 #1
4 2083
SM wrote:
Hello,

Knowing the index position of an array, how can i get the next $key
available, if it exists. I've found a solution but I was wondering if
there was a shorcut in PHP.

i.e:

$cats = array (
456=>'myown',
186=>'getto',
714=>'many',
484=>'wondering',
775=>'ok'
);

In this example, I know that the index postion is 2. So the return
$key should be the next one available, if it exists... that is: 484.
This is how i coded:

$index = 2;
$id = array_slice($cats, $index, 1, true);

foreach($id as $key=>$value) {
echo $key;
}

I've look at all the functions on the php web site and couldn't find a
shortcut to this.

Any ideas?

Thanks in advance
Marco
Hi Marco,
I think I understand what you are trying to do. Why not increase index
by 1 in the array slice function?

A quicker/nicer way to return the current key of an array, is to use the
function key(), rather than using a foreach loop.

Heres what I came up with

$index = 2; //known index

$id = array_slice($cats, $index + 1, 1, true); // splice, the next index

echo key($id); echo the key

Alternatively, you could condense it down a line:

$index = 2; //known index

echo key(array_slice($cats, $index + 1, 1, true)); // echo out the next
index

Hope this helps, Cheers
Leigh Finch
www.phpmaniac.net
Jun 2 '08 #3
On 25 Mai, 07:54, SM <servandomont...@gmail.comwrote:
Hello,

Knowing the index position of an array, how can i get the next $key
available, if it exists. I've found a solution but I was wondering if
there was a shorcut in PHP.

i.e:

$cats = array (
* * * * 456=>'myown',
* * * * 186=>'getto',
* * * * 714=>'many',
* * * * 484=>'wondering',
* * * * 775=>'ok'
);

In this example, I know that the index postion is 2. So the return
$key should be the next one available, if it exists... that is: 484.
This is how i coded:

$index = 2;
$id = array_slice($cats, $index, 1, true);

foreach($id as $key=>$value) {
* * * * echo $key;

}

I've look at all the functions on the php web site and couldn't find a
shortcut to this.

Any ideas?

Thanks in advance
Marco
$cats_keys = array_keys($cats);

echo $cats_keys[3];

?
Jun 2 '08 #4
SM
On May 25, 6:32 am, Mikhail Kovalev <mikhail_kova...@mail.ruwrote:
On 25 Mai, 07:54, SM <servandomont...@gmail.comwrote:
Hello,
Knowing the index position of an array, how can i get the next $key
available, if it exists. I've found a solution but I was wondering if
there was a shorcut in PHP.
i.e:
$cats = array (
456=>'myown',
186=>'getto',
714=>'many',
484=>'wondering',
775=>'ok'
);
In this example, I know that the index postion is 2. So the return
$key should be the next one available, if it exists... that is: 484.
This is how i coded:
$index = 2;
$id = array_slice($cats, $index, 1, true);
foreach($id as $key=>$value) {
echo $key;
}
I've look at all the functions on the php web site and couldn't find a
shortcut to this.
Any ideas?
Thanks in advance
Marco

$cats_keys = array_keys($cats);

echo $cats_keys[3];

?
Thanks guys. I've tried a combination of all the solutions proposed
and it work perfectly!

Marco
Jun 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: M. Clift | last post by:
Hi All, Can someone tell me is there a shorthand version to do this? l1 = Names = {'n1':'Cuthbert','n2' :'Grub','n3' :'Dibble' etc...} for name in l1: print Names,
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
24
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
2
by: nickdevx | last post by:
Hi Say if I have a mixed array: $array = array("item1", "2"=>"item2", "5", "item4key"=>"item4") Is it possible while looping through the array (foreach ($array as $key=>$val)) to check if...
1
by: stinehelferw | last post by:
I need a right-click menu on a form list control with combobox controls. I used View->Toolbars->Customize to create a toolbar. I added ComboBox through commands tab. I set properties to Popup...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.