LoginSignup
1
1

More than 3 years have passed since last update.

PHP array_mapの色々活用方法

Last updated at Posted at 2020-11-22

初めに

@tadsan さんに他の書き方教えていただいたので追記しています!
@tadsan さんありがとうございます!:bow:

比較
こんな時どうしよう、、、
こんな時どうしてたっけ?
みたいなことが10000億回ぐらいあるので、
ここに記録しておくことにしました!

記事は温かい目でみて頂けると嬉しいです!
優しい世界大事、大事ヽ(´▽`)/

ちなみに、コード実際にすぐ動かしたいだ!という方に以下オススメです!
私はよくお世話になってます!『paiza』さんありがとうございます!
https://paiza.io/ja/projects/new

in_array + array_map

:thinking:配列の中のあるキーの値の中に含まれているか確認したいんだ~~~~の時に使用

コード例

<?php
$user_article_id = 454556;

$stars = [
    0 => ['id' => 123424, 'article_id' => 454545],
    1 => ['id' => 198989, 'article_id' => 454556],
    2 => ['id' => 132323, 'article_id' => 454547]
];

// article_idの配列作成
$articleIds = array_map(fn($el) => ($el['article_id']), $stars);

//『articleIds』に『user_article_id』が含まれているかの確認
if(in_array($user_article_id, $articleIds)){
    echo '含まれている';
}else{
    echo '含まれていない';
}
//含まれている

上記を『array_column』を使って書くと以下になります!
@tadsan さんに教えていただきました!

<?php
$user_article_id = 454556;

$stars = [
    0 => ['id' => 123424, 'article_id' => 454545],
    1 => ['id' => 198989, 'article_id' => 454556],
    2 => ['id' => 132323, 'article_id' => 454547]
];

// array($article_id => $id) 配列作成
$articleIds = array_column($stars, 'id', 'article_id');

//『articleIds』に『user_article_id』が含まれているかの確認
if (isset($articleIds[$user_article_id])){
    echo '含まれている';
} else {
    echo '含まれていない';
}
//含まれている

array_map + count + array_sum

:thinking:配列の中のあるキーの値の配列の合計数を出したいのどうすれば良いかな?

<?php
$stars = [
    0 => ['ids' => [123424, 343434], 'article_id' => 454545],
    1 => ['ids' => [198989, 545454], 'article_id' => 454556],
    2 => ['ids' => [132323, 767676, 87878], 'article_id' => 454547]
];

// 各idsの配列の合計を格納した配列の作成
$idCounts = array_map(fn($el) => count($el['ids']), $stars);

//上記の配列数を足す
$star_id_total =  array_sum($idCounts);

echo $star_id_total; // => 7

上記を『array_reduce()』を使って書くと以下になります!
@tadsan さんに教えていただきました!

<?php
$stars = [
    0 => ['ids' => [123424, 343434], 'article_id' => 454545],
    1 => ['ids' => [198989, 545454], 'article_id' => 454556],
    2 => ['ids' => [132323, 767676, 87878], 'article_id' => 454547]
];

$star_id_total = array_reduce($stars, fn($sum, $el) => $sum + count($el['ids']));

var_dump($star_id_total); // => 7

ちなみにこれをforeachだけで書くとしたらのサンプルコード1

<?php
$stars = [
    0 => ['ids' => [123424, 343434], 'article_id' => 454545],
    1 => ['ids' => [198989, 545454], 'article_id' => 454556],
    2 => ['ids' => [132323, 767676, 87878], 'article_id' => 454547]
];
$count = 0;
foreach ($stars as $star){
    $ids = $star['ids'];
    foreach ($ids as $id){
        $count += 1;
    }
}
echo $count;  // => 7

ちなみにこれをforeachだけで書くとしたらのサンプルコード2

<?php
$stars = [
    0 => ['ids' => [123424, 343434], 'article_id' => 454545],
    1 => ['ids' => [198989, 545454], 'article_id' => 454556],
    2 => ['ids' => [132323, 767676, 87878], 'article_id' => 454547]
];
$count = 0;
foreach ($stars as $star){
    $idCount = count($star['ids']);
    $count += $idCount;
}
echo $count;  // => 7

最後に

終わりです!
また少し凝ったやつあったら書き足していく予定です!
ちなみに私はこの書き方が絶対だという感じのものはないです。

1
1
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
1
1