PHPの標準関数をRubyで書く（配列操作編）

## はじめに

PHPの標準関数をRubyで書いてみました。
簡単に書けそうなものだけパズル感覚で書いてます。

配列操作編です。

PHPの添字配列はArray、連想配列はHashに対応させました。
特に差し支えなさそうなケースでは他の型でも許容としています。（返り値がEnumeratorになる等）

引数と返り値の型はテーブルで記載しました。

## array_chunk($array, $chunk)
[PHP: array_chunk - Manual](https://www.php.net/manual/ja/function.array-chunk.php)

|||
---|---
array|Array
chunk|Integer
return|Enumerator
```ruby:Ruby
array.each_slice(chunk)
```

|||
---|---
array|Hash
chunk|Integer
return|Hash
```ruby:Ruby
array.each_slice(chunk).map(&:to_h)
```

## array_column($array, $column_key)
[PHP: array_column - Manual](https://www.php.net/manual/ja/function.array-column.php)

|||
---|---
array|Array&lt;Array &#124; Hash&gt;
column_key|untyped
return|Enumerator
```ruby:Ruby
array.map { |v| v[column_key] }
```

|||
---|---
array|Hash&lt;untyped, Array &#124; Hash&gt;
column_key|untyped
return|Hash
```ruby:Ruby
array.transform_values { |v| v[column_key] }
```

## array_combine($keys, $values)
[PHP: array_combine - Manual](https://www.php.net/manual/ja/function.array-combine.php)

|||
---|---
keys|Array
values|Array
return|Hash
```ruby:Ruby
keys.zip(values).to_h
```

## array_count_values($array)
[PHP: array_count_values - Manual](https://www.php.net/manual/ja/function.array-count-values.php)

|||
---|---
array|Array
return|Hash
```ruby:Ruby
array.tally
```

|||
---|---
array|Hash
return|Hash
```ruby:Ruby
array.values.tally
```

## array_diff($array, ...$arrays )
[PHP: array_diff - Manual](https://www.php.net/manual/ja/function.array-diff.php)

※入力がArrayのケースは省略。返り値のindexが振り直されず飛び飛びになる為。

|||
---|---
array|Hash
arrays|Array&lt;Hash&gt;
return|Hash
```ruby:Ruby
array.filter { |_, v| arrays.flat_map(&:values).none? { |vv| v == vv } }
```

## array_fill_keys($keys, $value)
[PHP: array_fill_keys - Manual](https://www.php.net/manual/ja/function.array-fill-keys.php)

|||
---|---
keys|Array
value|untyped
return|Hash
```ruby:Ruby
keys.zip([value].cycle).to_h
```

## array_fill($start_index, $count, $value)
[PHP: array_fill - Manual](https://www.php.net/manual/ja/function.array-fill.php)

|||
---|---
start_index|0
count|Integer
value|untyped
return|Array
```ruby:Ruby
[value] * count
```

|||
---|---
start_index|Integer ※0以上
count|Integer
value|untyped
return|Hash
```ruby:Ruby
(start_index..).take(count).zip([value].cycle).to_h
```

## array_filter($array, $callback, $mode)
[PHP: array_filter - Manual](https://www.php.net/manual/ja/function.array-filter.php)

※入力がArrayのケースは省略。返り値のindexが振り直されず飛び飛びになる為。

|||
---|---
array|Hash
callback|Proc
mode|ARRAY_FILTER_USE_BOTH
return|Hash
```ruby:Ruby
array.filter { |k, v| callback.(v, k) }
```

## array_flip($array)
[PHP: array_flip - Manual](https://www.php.net/manual/ja/function.array-flip.php)

|||
---|---
array|Array
return|Hash
```ruby:Ruby
array.map.with_index.to_h
```

|||
---|---
array|Hash
return|Hash
```ruby
array.invert
```

## array_intersect($array, ...$arrays)
[PHP: array_intersect - Manual](https://www.php.net/manual/ja/function.array-intersect.php)

※入力がArrayのケースは省略。返り値のindexが振り直されず飛び飛びになる為。

|||
---|---
array|Hash
array|Array&lt;Hash&gt;
return|Hash
```ruby:Ruby
array.filter { |_, v| arrays.flat_map(&:values).any? { |vv| v == vv } }
```

## array_key_exists($key, $array)
[PHP: array_key_exists - Manual](https://www.php.net/manual/ja/function.array-key-exists.php)

|||
---|---
key|Integer
array|Array
return|TrueClass &#124; FalseClass
```ruby:Ruby
array.each_index.include?(key)
```

|||
---|---
key|Integer
array|Hash
return|TrueClass &#124; FalseClass
```ruby:Ruby
array.key?(key)
```

## array_key_first($array)
[PHP: array_key_first - Manual](https://www.php.net/manual/ja/function.array-key-first.php)

|||
---|---
array|Array
return|Integer
```ruby:Ruby
0
```

|||
---|---
array|Hash
return|untyped
```ruby:Ruby
array.keys.first
```

## array_key_last($array)
[PHP: array_key_last - Manual](https://www.php.net/manual/ja/function.array-key-last.php)

|||
---|---
array|Array
return|Integer
```ruby:Ruby
array.count.pred
```

|||
---|---
array|Hash
return|untyped
```ruby:Ruby
array.keys.last
```

## array_keys($array)
[PHP: array_keys - Manual](https://www.php.net/manual/ja/function.array-keys.php)

|||
---|---
array|Array
return|Enumerator
```ruby:Ruby
array.each_index
```

|||
---|---
array|Hash
return|Array
```ruby:Ruby
array.keys
```

## array_map($callback, $array, ...$arrays)
[PHP: array_map - Manual](https://www.php.net/manual/ja/function.array-map.php)

|||
---|---
callback|Proc
array|Array
arrays|Array&lt;Array&gt;
return|Enumerator
```ruby:Ruby
array.zip(*arrays).map(&callback)
```

|||
---|---
callback|Proc
array|Hash
arrays|Array&lt;Hash&gt;
return|Enumerator
```ruby:Ruby
array.values.zip(*arrays.map(&:values)).map(&callback)
```

## array_merge(...$arrays)
[PHP: array_merge - Manual](https://www.php.net/manual/ja/function.array-merge.php)

|||
---|---
arrays|Array&lt;Array&gt;
return|Array
```ruby:Ruby
arrays.inject(:+)
```

|||
---|---
arrays|Array&lt;Hash&gt;
return|Hash
```ruby:Ruby
arrays.inject(:merge)
```

## array_pad($array, $length, $value)
[PHP: array_pad - Manual](https://www.php.net/manual/ja/function.array-pad.php)

|||
---|---
array|Array
length|Integer ※0以上
value|untyped
return|Array
```ruby:Ruby
array + [value] * [length - array.count, 0].max
```

|||
---|---
array|Array
length|Integer ※0未満
value|untyped
return|Array
```ruby:Ruby
[value] * [-length - array.count, 0].max + array
```

## array_pop($array)
[PHP: array_pop - Manual](https://www.php.net/manual/ja/function.array-pop.php)

|||
---|---
array|Array
return|untyped
```ruby:Ruby
array.pop
```

|||
---|---
array|Hash
return|untyped
```ruby:Ruby
array.delete(array.keys.last)
```

## array_product($array)
[PHP: array_product - Manual](https://www.php.net/manual/ja/function.array-product.php)

|||
---|---
array|Array&lt;Integer &#124; Float&gt;
return|Integer &#124; Float
```ruby:Ruby
array.inject(:*)
```

## array_push(&$array, ...$values)
[PHP: array_push - Manual](https://www.php.net/manual/ja/function.array-push.php)

|||
---|---
array|Array
values|untyped
return|Array
```ruby:Ruby
array.push(*values)
```

## array_rand($array, $num)
[PHP: array_rand - Manual](https://www.php.net/manual/ja/function.array-rand.php)

|||
---|---
array|Array
num|Integer
return|Array
```ruby:Ruby
array.each_index.to_a.sample(num).sort
```

|||
---|---
array|Hash
num|Integer
return|Array
```ruby:Ruby
array.keys.sample(num).sort_by { |v| array.keys.index(v) }
```

## array_reduce($array, $callback, $initial)
[PHP: array_reduce - Manual](https://www.php.net/manual/ja/function.array-reduce.php)

|||
---|---
array|Array
callback|Proc
initial|untyped
return|untyped
```ruby:Ruby
array.inject(initial, &callback)
```

|||
---|---
array|Hash
callback|Proc
initial|untyped
return|untyped
```ruby:Ruby
array.values.inject(initial, &callback)
```

## array_reverse($array, $preserve_key)
[PHP: array_reverse - Manual](https://www.php.net/manual/ja/function.array-reverse.php)

|||
---|---
array|Array
preserve_key|FalseClass
return|Enumerator
```ruby:Ruby
array.reverse
```

|||
---|---
array|Hash
preserve_key|TrueClass
return|Hash
```ruby:Ruby
array.reverse_each.to_h
```

## array_search($needle, $haystack, $strict)
[PHP: array_search - Manual](https://www.php.net/manual/ja/function.array-search.php)

|||
---|---
needle|untyped
haystack|Array
strict|※言語仕様が違うので省略
return|Integer
```ruby:Ruby
haystack.index(needle)
```

|||
---|---
needle|untyped
haystack|Hash
strict|※言語仕様が違うので省略
return|untyped
```ruby:Ruby
haystack.key(needle)
```

## array_shift(&$array)
[PHP: array_shift - Manual](https://www.php.net/manual/ja/function.array-shift.php)

|||
---|---
array|Array
return|untyped
```ruby:Ruby
array.shift
```

|||
---|---
array|Hash
return|untyped
```ruby:Ruby
array.shift.then { |v| array.empty? ? v : v.last }
```

## array_slice($array, $offset, $length, $preserve_keys)
[PHP: array_slice - Manual](https://www.php.net/manual/ja/function.array-slice.php)

|||
---|---
array|Array
offset|Integer
length|Integer ※0以上
preserve_keys|FalseClass
return|Array
```ruby:Ruby
array[offset, length]
```

|||
---|---
array|Hash
offset|Integer
length|Integer ※0以上
preserve_keys|TrueClass
return|Hash
```ruby:Ruby
array.slice(*array.keys[offset, length])
```

## array_sum($array)
[PHP: array_sum - Manual](https://www.php.net/manual/ja/function.array-sum.php)

|||
---|---
array|Array
return|Integer &#124; Float
```ruby:Ruby
array.sum
```

|||
---|---
array|Hash
return|Integer &#124; Float
```ruby:Ruby
array.values.sum
```

## array_unique($array, $flags)
[PHP: array_unique - Manual](https://www.php.net/manual/ja/function.array-unique.php)

|||
---|---
array|Array
flags|※ソート機能はないものとして省略
return|Array
```ruby:Ruby
array.uniq
```

※重複した要素は前の要素が残る（PHPのarray_uniqueと同様）

|||
---|---
array|Hash
flags|※ソート機能はないものとして省略
return|Hash
```ruby:Ruby
array.invert.invert
```

※重複した要素は後の要素が残る（前を残すならreverse_eachを挟む）

## array_unshift(&$array, ...$values)
[PHP: array_unshift - Manual](https://www.php.net/manual/ja/function.array-unshift.php)

|||
---|---
array|Array
return|Integer
```ruby:Ruby
(array = array.unshift(*values)).count
```

## array_values($array)
[PHP: array_values - Manual](https://www.php.net/manual/ja/function.array-values.php)

|||
---|---
array|Hash
return|Array
```ruby:Ruby
array.values
```

## array_walk(&$array, $callback, $userdata)
[PHP: array_walk - Manual](https://www.php.net/manual/ja/function.array-walk.php)

|||
---|---
array|Array
callback|Proc
userdata|untyped
return|TrueClass
```ruby:Ruby
true | array.each_with_index(&->(*vs){ vs << userdata } >> callback)
```

|||
---|---
array|Hash
callback|Proc
userdata|untyped
return|TrueClass
```ruby:Ruby
true | array.each(&->(kv){ kv.reverse << userdata } >> callback)
```

## in_array($needle, $haystack, $strict)
[PHP: in_array - Manual](https://www.php.net/manual/ja/function.in-array.php)

|||
---|---
needle|untyped
haystack|Array
strict|※言語仕様が違うので省略
```ruby:Ruby
haystack.include?(needle)
```

|||
---|---
needle|untyped
haystack|Hash
strict|※言語仕様が違うので省略
```ruby:Ruby
haystack.value?(needle)
```

## 参考文献

- Class array (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Array.html

- Class hash (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Hash.html

- Module enumerable (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Enumerable.html

- Array_chunk. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-chunk.php

- Array_column. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-column.php

- Array_combine. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-combine.php

- Array_count_values. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-count-values.php

- Array_diff. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-diff.php

- Array_fill_keys. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-fill-keys.php

- Array_fill. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-fill.php

- Array_filter. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-filter.php

- Array_flip. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-flip.php

- Array_intersect. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-intersect.php

- Array_key_exists. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-key-exists.php

- Array_key_first. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-key-first.php

- Array_key_last. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-key-last.php

- Array_keys. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-keys.php

- Array_map. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-map.php

- Array_merge. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-merge.php

- Array_pad. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-pad.php

- Array_pop. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-pop.php

- Array_product. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-product.php

- Array_push. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-push.php

- Array_rand. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-rand.php

- Array_reduce. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-reduce.php

- Array_reverse. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-reverse.php

- Array_search. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-search.php

- Array_shift. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-shift.php

- Array_slice. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-slice.php

- Array_sum. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-sum.php

- Array_unique. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-unique.php

- Array_unshift. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-unshift.php

- Array_values. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-values.php

- Array_walk. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.array-walk.php

- In_array. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.in-array.php