0

My php reads in xml and I want to ouput the values within a given range. I have no way of knowing the size of the array or the range. However, I do know where to start; I have a $key that holds my current location. I also know where to stop; I have the word "ENDEVENTS" between each set. I want to get the values from my current position ($key) to my end position ("ENDEVENTS").

for example i may have an array set like this:

Array(
 [0]=1
 [1]=apple
 [2]=straw
 [3]=bike
 [4]=ENDEVENTS
 [5]=15
 [6]=hair
 [7]=shirt
 [8]=nose
 [9]=kiwi
 [10]=ENDEVENTS
 )

My code knows when I'm on 15 (in this example $key=5). I want to print 6 through 9. I tried using foreach but it's thats causing issues with what I'm trying to produce.

Any ideas?

2 Answers 2

1

Not so sure if i understood all ok but, ill give a try :-D

while(array[$key]!="ENDEVENTS"){
    echo array[$key];
    $key++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

shows the first value but I think I figure out how to get rid of it.
0

Not entirely sure if I understand your question, but maybe this is helpful:

$stuff_to_print = array_slice($my_array,$key,array_search('ENDEVENTS',$my_array));

This should return an array with key values from the $key to the next 'ENDEVENTS' value

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.