关联数组(或哈希)php 到 ruby
我对 ruby 比较陌生,而且我来自 php。我似乎不明白的一件事是哈希和数组之间的区别。 如何在 ruby 中实现关联数组? 例如。
$albums = array();
$songs = array();
$songs[] = array('title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 2', 'artist' => 'Artist 2', 'album' => 'Album 2');
$songs[] = array('title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1');
foreach($songs as $song)
{
if( !isset($albums[$song['album']]) ) $albums[$song['album']] = array();
$albums[$song['album']]['title'] = $song['album'];
$albums[$song['album']]['songs'] = $song;
}
print_r($albums);
['Album 1']
=> 'title' => 'Album 1'
=> 'songs'
=> 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'
=> 'title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1'
=> 'title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1'
=> 'title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1'
['Album 2']
=> 'title' => 'Album 2'
=> 'songs'
=> 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'
或者甚至,我可以声明一个包含标题、艺术家和歌曲数量的数组专辑信息,而不是标题和歌曲。
我怎样才能用红宝石做类似的事情?我会用什么来实现这个目标?
I'm relatively new to ruby and i came from php. One thing that it seems i would not understand is the difference between hashes and arrays.
How do you implement associative arrays in ruby?
Eg.
$albums = array();
$songs = array();
$songs[] = array('title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 2', 'artist' => 'Artist 2', 'album' => 'Album 2');
$songs[] = array('title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1');
foreach($songs as $song)
{
if( !isset($albums[$song['album']]) ) $albums[$song['album']] = array();
$albums[$song['album']]['title'] = $song['album'];
$albums[$song['album']]['songs'] = $song;
}
print_r($albums);
['Album 1']
=> 'title' => 'Album 1'
=> 'songs'
=> 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'
=> 'title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1'
=> 'title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1'
=> 'title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1'
['Album 2']
=> 'title' => 'Album 2'
=> 'songs'
=> 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'
Or even, instead of title and songs i can declare an array album info with title, artist and how many songs.
How can i do something similar with ruby? what i would use to accomplish that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 PHP 或多或少直接重写:
更简单的是单行:
数组只有整数键,并将所有键分配给最高的键(因此,如果您分配 foo[100] = 1 ,键 0-99 的位置也会占用内存(带有
nil
值);您用方括号记下数组值:[1, 2, 3] 是一个 3 元素数组,
[]
为空,哈希值括在大括号
{}
中,并且可以包含几乎任何内容作为键,但是块可以。也可以用大括号书写,因此请注意不要混淆两者。A more-or-less direct rewrite from your PHP:
Easier would be the one-liner:
Arrays have only integer keys, and allocate all of them up to the highest one (so if you assign
foo[100] = 1
, there will be locations for keys 0-99 also occupying memory (withnil
value); you note array values with square brackets:[1, 2, 3]
is a 3-element array,[]
is empty.Hashes are enclosed in curly braces
{}
, and can have pretty much anything as keys. However, blocks can also be written with braces, so take care not to confuse the two.您应该能够像使用 PHP 关联数组一样使用 Ruby 哈希。
You should be able to use a Ruby Hash exactly like a PHP associative array.
正如您在问题标题中提到的,关联数组在 Ruby 中通常称为哈希。语法与数组略有不同。
As you mentioned in the title of your question, associative arrays are usually referred to as hashes in Ruby. The syntax is slightly different from arrays.
你可以在一行中完成它
只是我不明白为什么你使用专辑标题两次:作为哈希键和作为标题参数?我认为你应该只使用一次:)
You can do it in one line
Only I don't understand why do you use Album title twice: as a hash key and as a title param? You should use only once, I think :)