Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 76365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:39:43+00:00 2026-05-10T20:39:43+00:00

PHP, for all its warts, is pretty good on this count. There’s no difference

  • 0

PHP, for all its warts, is pretty good on this count. There’s no difference between an array and a hash (maybe I’m naive, but this seems obviously right to me), and to iterate through either you just do

foreach (array/hash as $key => $value) 

In Ruby there are a bunch of ways to do this sort of thing:

array.length.times do |i| end  array.each  array.each_index  for i in array 

Hashes make more sense, since I just always use

hash.each do |key, value| 

Why can’t I do this for arrays? If I want to remember just one method, I guess I can use each_index (since it makes both the index and value available), but it’s annoying to have to do array[index] instead of just value.


Oh right, I forgot about array.each_with_index. However, this one sucks because it goes |value, key| and hash.each goes |key, value|! Is this not insane?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:39:44+00:00Added an answer on May 10, 2026 at 8:39 pm

    This will iterate through all the elements:

    array = [1, 2, 3, 4, 5, 6] array.each { |x| puts x }  # Output:  1 2 3 4 5 6 

    This will iterate through all the elements giving you the value and the index:

    array = ["A", "B", "C"] array.each_with_index {|val, index| puts "#{val} => #{index}" }  # Output:  A => 0 B => 1 C => 2 

    I’m not quite sure from your question which one you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP treats all arrays as associative, so there aren't any built in functions. Can
How can I pass a Javascript Array via JQuery Post so that all its
it's all in index.php /* Define site root */ defined('DOCUMENT_ROOT') ? null : define('DOCUMENT_ROOT',realpath(dirname(__FILE__)));
I'm using PHP to scrape a website and collect some data. It's all done
I am not familiar with PHP at all and had a quick question. I
I am using a function in php for all select queries so that i
I would include a php header ( mysite.com/header.php ) in all the pages from
I'm looking to parse some CXML in PHP...basically all I'm looking to get the
I'm working on Ubuntu.(Linux) I want to display all .php pages to .html in
First of all, I'm not much of a programmer if at all. This question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.