Jump to content

Looping array (get key and value)


LittleJoe

Recommended Posts

I need to loop through an array, in the order that the items appear, and get both the key of the item and its value. I know I can use foreach to loop through an array but as far as I know I can choose between the key and the value of the current item but not get both.

 

Here's an example of an array:

<?php$SampleArray = array(55 => "car","2" => "house","boat" => "boat",0 => "engine",666 => "website",);?>

It would work somehow like this:

 

loop($SampleArray){ echo key . " - " value . "n";}

 

And the result would be something like:55 - car

2 - house

boat - boat

0 - engine

666 - website

Edited by LittleJoe
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...