dcp3450
dcp3450

Reputation: 11187

How do I print array values in a range when values are supplied?

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?

Upvotes: 0

Views: 1129

Answers (2)

deadkarma
deadkarma

Reputation: 3144

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

Upvotes: 0

kviksilver
kviksilver

Reputation: 3854

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

while(array[$key]!="ENDEVENTS"){
    echo array[$key];
    $key++;
}

Upvotes: 1

Related Questions