LoginSignup
13
14

More than 5 years have passed since last update.

array_map(null, ...) で配列を結合

Last updated at Posted at 2013-05-07

@fivestr @shimooka array_map(null, $ids, $names); とかは? Python だと 組み込みの zip() で一撃なんだけど、PHP には array_zip() ないんすね。

- https://twitter.com/heavenshell/status/331705230582501377

$ids = [0, 1, 2];
$names = ['foo', 'bar', 'baz'];

から

[ [0, 'foo'], [1, 'bar'], [2, 'bar'] ]

を組み立ててくれるようなPHPの関数知りませんか?

- https://twitter.com/fivestr/status/331702099140308992

array_map(null) !? 予想外のリプライが来たので試してみた:

<?php

var_dump(array_map(null, [1, 2, 3], ['foo', 'bar', 'baz'], ['x', 'y', 'z']));

結果:

array(3) {
  [0] =>
  array(3) {
    [0] =>
    int(1)
    [1] =>
    string(3) "foo"
    [2] =>
    string(1) "x"
  }
  [1] =>
  array(3) {
    [0] =>
    int(2)
    [1] =>
    string(3) "bar"
    [2] =>
    string(1) "y"
  }
  [2] =>
  array(3) {
    [0] =>
    int(3)
    [1] =>
    string(3) "baz"
    [2] =>
    string(1) "z"
  }
}

できてる...。そんな使い方があったのか array_map。さすがに知らなかった。

これか。マニュアルにもあった
http://www.php.net/manual/ja/function.array-map.php#example-4835

- https://twitter.com/shimooka/status/331707431379927040

なるほど、マニュアルにも書いてある模様。。。

13
14
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14