0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

配列にid的なのつけたいな

Last updated at Posted at 2022-09-01

こういう配列

$tests = array(
    array(
        'price' => 100,
        'name' => 'hoge'),

    array(
        'price' => 200,
        'name' => 'huga'),

    array(
        'price' => 300,
        'name' => 'piyo')
        );

var_dump($tests);

indexあるけど、1から始まるid付けたい

こうした

foreach($tests as $index => $test){
    $id = 1 + $index;
    $tests[$index] = array_merge($test,array('id'=>$id));
}

var_dump($tests);

実行結果

array(3) {
  [0]=>
  array(3) {
    ["price"]=>
    int(100)
    ["name"]=>
    string(4) "hoge"
    ["id"]=>
    int(1)
  }
  [1]=>
  array(3) {
    ["price"]=>
    int(200)
    ["name"]=>
    string(4) "huga"
    ["id"]=>
    int(2)
  }
  [2]=>
  array(3) {
    ["price"]=>
    int(300)
    ["name"]=>
    string(4) "piyo"
    ["id"]=>
    int(3)
  }
}

idついたなあ

そもそも

PHPの公式マニュアル見ると
https://www.php.net/manual/ja/function.array.php

$tests = array(
    1 => array(
        'price' => 100,
        'name' => 'hoge'),

    array(
        'price' => 200,
        'name' => 'huga'),

    array(
        'price' => 300,
        'name' => 'piyo')
        );

これで1からindexが始まる配列を作成できるので
foreachの部分はこうなる

foreach($tests as $index => $test){
    $tests[$index] = array_merge($test,array('id'=>$index));
}

自分のプロジェクトでは、配列を$test=array()みたいな感じで定義するということがなかったので、最初の方式にした

連想配列だったらどうすんの

$tests = array(
    'hoo' => array(
        'price' => 100,
        'name' => 'hoge'),

    'ahh'=> array(
        'price' => 200,
        'name' => 'huga'),

    'poo'=> array(
        'price' => 300,
        'name' => 'piyo')
        ); 

foreach($tests as $index => $test){
    $tests[$index] = array_merge($test,array('id'=>$index));
}

var_dump($tests);

実行結果

array(3) {
  [0]=>
  array(3) {
    ["price"]=>
    int(100)
    ["name"]=>
    string(4) "hoge"
    ["id"]=>
    string(3) "hoo"
  }
  [1]=>
  array(3) {
    ["price"]=>
    int(200)
    ["name"]=>
    string(4) "huga"
    ["id"]=>
    string(3) "ahh"
  }
  [2]=>
  array(3) {
    ["price"]=>
    int(300)
    ["name"]=>
    string(4) "piyo"
    ["id"]=>
    string(3) "poo"
  }
}

それはそう

解決法

$index=1;
foreach($tests as $key => $test){
    $tests[$key] = array_merge($test,array('id'=>$index));
    $index++;
}

var_dump($tests);

実行結果

array(3) {
  ["hoo"]=>
  array(3) {
    ["price"]=>
    int(100)
    ["name"]=>
    string(4) "hoge"
    ["id"]=>
    int(1)
  }
  ["ahh"]=>
  array(3) {
    ["price"]=>
    int(200)
    ["name"]=>
    string(4) "huga"
    ["id"]=>
    int(2)
  }
  ["poo"]=>
  array(3) {
    ["price"]=>
    int(300)
    ["name"]=>
    string(4) "piyo"
    ["id"]=>
    int(3)
  }
}

なんか他にスマートな方法ありそう

終わりに

ベストプラクティスあったら教えて頂きたいです

0
0
3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?