0

Is there a way to get the the current position of an array from within a nested array ?

I have a php script that has a for loop which cycles through an array, with in this is an nested loop which cycle through a sub array. I can use pos() to get the position of the child array, is there anyway of getting the current position of the parent array.

I am sure there must be a way to do this, or is the best way to just create a counter?

thanks in advance

.k

2 Answers 2

3

If you're using a for loop, you already have a counter. In this example, it's $i :

for($i = 0; $i < $arrayLength; $i++) ...

If you're actually using a foreach loop, use the syntax that gives you the key:

foreach($array as $key => $value) ...
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Scott, It is a foreach loop, your suggestion done the job! Thanks .k
0

A PHP variable has no information from where it is referenced - due to references and copy-on-write there might be even more things (global/local variables, array elements, properties, ...) pointing to a single variables.

If you have a reference to the "parent" element you can use pos() on that, if not you have to handle this yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.