Jump to content

Looping array (get key and value)


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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...