{"id":3057,"date":"2023-05-20T15:17:17","date_gmt":"2023-05-20T06:17:17","guid":{"rendered":"https:\/\/took.jp\/?p=3057"},"modified":"2023-05-21T11:02:28","modified_gmt":"2023-05-21T02:02:28","slug":"golang-php-array","status":"publish","type":"post","link":"https:\/\/took.jp\/tech-blog\/golang-php-array\/","title":{"rendered":"PHP\u306e\u914d\u5217\u95a2\u6570\u3092Go\u8a00\u8a9e\u3067\u518d\u73fe\u3059\u308b + \u30b9\u30e9\u30a4\u30b9\u306e\u57fa\u672c\u64cd\u4f5c"},"content":{"rendered":"<p><span>Go\u8a00\u8a9e\u306f\u30b7\u30f3\u30d7\u30eb\u3067\u52b9\u7387\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3042\u308a\u3001\u30b9\u30e9\u30a4\u30b9\u306f\u305d\u306e\u4e2d\u3067\u3082\u91cd\u8981\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002<\/span><\/p>\n<p><span>PHP\u3067\u306f\u914d\u5217\u95a2\u6570\u304c\u8c4a\u5bcc\u306b\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001\u305d\u306e\u591a\u304f\u306f\u30b9\u30e9\u30a4\u30b9\u306e\u64cd\u4f5c\u3092\u5bb9\u6613\u306b\u3057\u307e\u3059\u3002<\/span><\/p>\n<p><span>\u672c\u8a18\u4e8b\u3067\u306f\u3001Go\u8a00\u8a9e\u3067PHP\u306e\u3044\u304f\u3064\u304b\u306e\u914d\u5217\u95a2\u6570\u3092\u518d\u73fe\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2>\u30b9\u30e9\u30a4\u30b9\u306e\u57fa\u672c\u64cd\u4f5c<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-other\">\/\/ \u30b9\u30e9\u30a4\u30b9\u306e\u4f5c\u6210\r\nslice := make([]int, 0, 6)\r\nslice = append(slice, 1, 2, 3, 4)\r\n\r\n\/\/ \u6700\u5f8c\u306b\u8981\u7d20\u306e\u8ffd\u52a0\r\nslice = append(slice, 5)\r\nfmt.Println(slice) \/\/ [1, 2, 3, 4, 5]\r\n\r\n\/\/ \u6307\u5b9a\u4f4d\u7f6e\u306b\u8981\u7d20\u8ffd\u52a0\r\nindex := 1\r\naddValue := 9\r\nslice = append(slice[:index], append([]int{addValue}, slice[index:]...)...)\r\nfmt.Println(slice) \/\/ [1, 9, 2, 3, 4, 5]\r\n\r\n\/\/ \u7279\u5b9a\u306e\u8981\u7d20\u306e\u524a\u9664\r\nslice = append(slice[:index], slice[index+1:]...)\r\nfmt.Println(slice) \/\/ [1, 2, 3, 4, 5]\r\n\r\n\/\/ \u6700\u5f8c\u306e\u8981\u7d20\u306e\u524a\u9664\r\nslice = slice[:len(slice)-1]\r\nfmt.Println(slice) \/\/ [1, 2, 3, 4]\r\n\r\n\/\/ \u8981\u7d20\u306e\u30a2\u30af\u30bb\u30b9\r\nvalue := slice[index]\r\nfmt.Println(value) \/\/ 2<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30b9\u30e9\u30a4\u30b9\u306e\u5bb9\u91cf\u3068\u9577\u3055<\/h2>\n<p><span>\u30b9\u30e9\u30a4\u30b9\u306f\u5bb9\u91cf\uff08capacity\uff09\u3068\u9577\u3055\uff08length\uff09\u3068\u3044\u30462\u3064\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6301\u3061\u307e\u3059\u3002<\/span><\/p>\n<p><span>\u5bb9\u91cf\u306f\u30b9\u30e9\u30a4\u30b9\u304c\u683c\u7d0d\u3067\u304d\u308b\u8981\u7d20\u6570\u3092\u8868\u3057\u3001\u9577\u3055\u306f\u30b9\u30e9\u30a4\u30b9\u304c\u73fe\u5728\u4fdd\u6301\u3057\u3066\u3044\u308b\u8981\u7d20\u6570\u3092\u8868\u3057\u307e\u3059\u3002<\/span><\/p>\n<p><span>\u5185\u90e8\u914d\u5217\u306e\u30ad\u30e3\u30d1\u30b7\u30c6\u30a3\u3092\u8d85\u3048\u308b\u8981\u7d20\u6570\u304c\u8ffd\u52a0\u3055\u308c\u308b\u3068\u3001Go\u30e9\u30f3\u30bf\u30a4\u30e0\u306f\u3088\u308a\u5927\u304d\u3044\u30ad\u30e3\u30d1\u30b7\u30c6\u30a3\u306e\u65b0\u3057\u3044\u5185\u90e8\u914d\u5217\u3092\u5272\u308a\u5f53\u3066\u3001\u65e2\u5b58\u306e\u8981\u7d20\u3092\u305d\u3061\u3089\u306b\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002<\/span><\/p>\n<p><span>\u305d\u306e\u305f\u3081\u3001\u4e88\u3081\u5341\u5206\u306a\u30ad\u30e3\u30d1\u30b7\u30c6\u30a3\u3092\u6301\u3064\u30b9\u30e9\u30a4\u30b9\u3092\u4f5c\u6210\u3057\u3066\u304a\u304f\u3053\u3068\u3067\u3001\u8981\u7d20\u306e\u8ffd\u52a0\u3084\u64cd\u4f5c\u306e\u6027\u80fd\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/span><\/p>\n<pre class=\"line-numbers\"><code class=\"language-other\">slice := []int{1, 2, 3}\r\nfmt.Println(cap(slice))\r\nfmt.Println(len(slice))<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>PHP\u914d\u5217\u95a2\u6570\u3092\u518d\u73fe\u3059\u308b<\/h2>\n<h3>array_push<\/h3>\n<p>\u4e00\u3064\u4ee5\u4e0a\u306e\u8981\u7d20\u3092\u914d\u5217\u306e\u6700\u5f8c\u306b\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-other\">func arrayPush[T any](slice []T, elements ...T) []T {\r\n\treturn append(slice, elements...)\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h4>\u5229\u7528\u4f8b<\/h4>\n<pre class=\"line-numbers\"><code class=\"language-other\">slice := []int{1, 2, 3} \r\nslice = arrayPush(slice, 4, 5) \r\nfmt.Println(slice) \/\/ [1, 2, 3, 4, 5]<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>array_pop<\/h3>\n<p>\u914d\u5217\u306e\u672b\u5c3e\u306e\u8981\u7d20\u3092\u53d6\u308a\u9664\u304f\u3002<\/p>\n<p>PHP\u3067\u306f\u914d\u5217\u304c\u7a7a\u306e\u6642\u306bnull\u3092\u8fd4\u3057\u307e\u3059\u304c\u3001golang\u3067\u306fnil\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u30bc\u30ed\u5024\u3092\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-other\">func arrayPop[T any](slice *[]T) T {\r\n\tif len(*slice) == 0 {\r\n\t\tvar zeroValue T\r\n\t\treturn zeroValue\r\n\t}\r\n\tlastIndex := len(*slice) - 1\r\n\tpoppedElement := (*slice)[lastIndex]\r\n\t*slice = (*slice)[:lastIndex]\r\n\treturn poppedElement\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h4>\u5229\u7528\u4f8b<\/h4>\n<pre class=\"line-numbers\"><code class=\"language-other\">\/\/ \u5229\u7528\u4f8b \r\nslice := []string{\"orange\", \"banana\", \"apple\", \"raspberry\"}\r\nfruit := arrayPop(&amp;slice) \r\nfmt.Println(slice) \/\/ [orange, banana, apple] \r\nfmt.Println(fruit) \/\/ raspberry<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>array_shift<\/h3>\n<p>\u914d\u5217\u306e\u5148\u982d\u304b\u3089\u8981\u7d20\u3092\u4e00\u3064\u53d6\u308a\u51fa\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-other\">func arrayShift[T any](slice *[]T) T {\r\n\tif len(*slice) == 0 {\r\n\t\tvar zeroValue T\r\n\t\treturn zeroValue\r\n\t}\r\n\tpoppedElement := (*slice)[0]\r\n\t*slice = (*slice)[1:]\r\n\treturn poppedElement\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h4>\u5229\u7528\u4f8b<\/h4>\n<pre class=\"line-numbers\"><code class=\"language-other\">\/\/ \u5229\u7528\u4f8b \r\nslice := []string{\"orange\", \"banana\", \"apple\", \"raspberry\"} \r\nfruit := arrayShift(&amp;slice) \r\nfmt.Println(slice) \/\/ [banana apple raspberry] \r\nfmt.Println(fruit) \/\/ orange<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>array_merge<\/h3>\n<p>\u3072\u3068\u3064\u307e\u305f\u306f\u8907\u6570\u306e\u914d\u5217\u3092\u30de\u30fc\u30b8\u3059\u308b\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-other\">func arrayMerge[T any](slices ...[]T) []T {\r\n\tmerged := []T{}\r\n\tfor _, slice := range slices {\r\n\t\tmerged = append(merged, slice...)\r\n\t}\r\n\treturn merged\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h4>\u5229\u7528\u4f8b<\/h4>\n<pre class=\"line-numbers\"><code class=\"language-other\">\/\/ \u5229\u7528\u4f8b \r\nslice1 := []int{1, 2, 3} \r\nslice2 := []int{4, 5, 6} \r\nmergedSlice := arrayMerge(slice1, slice2) \r\nfmt.Println(mergedSlice) \/\/ [1, 2, 3, 4, 5, 6]<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>array_chunk<\/h3>\n<p>\u914d\u5217\u3092\u5206\u5272\u3059\u308b\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-other\">func arrayChunk[T any](slice []T, size int) [][]T {\r\n\tlength := len(slice)\r\n\tchunks := int(math.Ceil(float64(length) \/ float64(size)))\r\n\tresult := make([][]T, 0, chunks)\r\n\r\n\tfor i := 0; i &lt; chunks; i++ {\r\n\t\tstart := i * size\r\n\t\tend := start + size\r\n\t\tif end &gt; length {\r\n\t\t\tend = length\r\n\t\t}\r\n\r\n\t\tresult = append(result, slice[start:end])\r\n\t}\r\n\r\n\treturn result\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h4>\u5229\u7528\u4f8b<\/h4>\n<pre class=\"line-numbers\"><code class=\"language-other\">\/\/ \u5229\u7528\u4f8b \r\nnumbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} \r\nresult := arrayChunk(numbers, 3) \r\nfmt.Println(result) \/\/ [[1, 2, 3] [4, 5, 6] [7, 8, 9] [10]]\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<div data-pm-slice=\"1 1 []\" data-en-clipboard=\"true\">\u672c\u8a18\u4e8b\u3067\u306f\u3001array_push\u3001array_pop\u3001array_shift\u3001array_merge\u3001array_chunk\u3068\u3044\u3063\u305fPHP\u306e\u914d\u5217\u64cd\u4f5c\u95a2\u6570\u306e\u518d\u73fe\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/div>\n<p>Golang\u3067\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u5f15\u6570\u3084\u3001\u8fd4\u308a\u5024\u306bnull\u8a31\u5bb9\u306e\u623b\u308a\u5024\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u7b49\u306e\u8a00\u8a9e\u7684\u306a\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u30b9\u30e9\u30a4\u30b9\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001Go\u8a00\u8a9e\u3067\u3082\u4fbf\u5229\u306a\u914d\u5217\u64cd\u4f5c\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u304a\u307e\u3051<\/h2>\n<p>\u53c2\u8003\u8cc7\u6599: <a href=\"https:\/\/zenn.dev\/mattn\/articles\/31dfed3c89956d\">https:\/\/zenn.dev\/mattn\/articles\/31dfed3c89956d<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u826f\u3044\u8a18\u4e8b\u304c\u3042\u3063\u305f\u305f\u3081\u3001\u5171\u6709\u3002<\/p>\n<p>append\u306b\u3088\u308a\u3001\u30b9\u30e9\u30a4\u30b9\u3092\u524a\u9664\u3059\u308b\u65b9\u6cd5\u306e\u5834\u5408\u3001\u6d88\u3057\u305f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u4ee5\u964d\u306e\u8981\u7d20\u3092\u524d\u306b\u305a\u3089\u3059\u5fc5\u8981\u304c\u3042\u308a\u3001\u5148\u982d\u306b\u3044\u304f\u7a0b\u3001\u524a\u9664\u306b\u304b\u304b\u308b\u6642\u9593\u306f\u9577\u304f\u306a\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u524a\u9664\u5f8c\u3001\u30b9\u30e9\u30a4\u30b9\u9806\u3092\u6c17\u306b\u3057\u306a\u3044\u30b1\u30fc\u30b9\u3067\u306f\u4e0b\u8a18\u306e\u8a18\u8ff0\u65b9\u6cd5\u304c\u826f\u3044\u3068\u89e3\u8aac\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote><p>a[i] = a[len(a)-1]\n<p>a = a[:len(a)-1]\n<p><cite class=\"blockquote_ref\"> <a href=\"https:\/\/zenn.dev\/mattn\/articles\/31dfed3c89956d\" target=\"_blank\" rel=\"noopener noreferrer\">Go \u8a00\u8a9e\u3067\u30b9\u30e9\u30a4\u30b9\u304b\u3089\u8981\u7d20\u3092\u6d88\u3059\u306b\u306f<\/a> <\/cite><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>\u4e0a\u8a18\u3067\u4f55\u3092\u3057\u3066\u3044\u308b\u306e\u304b\u3001\u30d1\u30c3\u3068\u898b\u3001\u7406\u89e3\u304c\u96e3\u3057\u304b\u3063\u305f\u305f\u3081\u3001\u4e0b\u8a18\u306b\u30b3\u30fc\u30c9\u3092\u8a18\u8f09\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-other\">numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\r\nindex := 3\r\n\r\nnumbers[index] = numbers[len(numbers)-1]\r\nfmt.Println(numbers) \/\/ [1, 2, 3, 10, 5, 6, 7, 8, 9, 10]\r\n\r\nnumbers = numbers[:len(numbers)-1]\r\nfmt.Println(numbers) \/\/ [1, 2, 3, 10, 5, 6, 7, 8, 9]<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u524a\u9664\u8981\u7d20\u306e\u5834\u6240\u3092\u672b\u5c3e\u306e\u5024\u3067\u7f6e\u304d\u63db\u3048\u3066\u3001\u672b\u5c3e\u306e\u8981\u7d20\u3092\u524a\u9664\u3057\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u78ba\u304b\u306b\u3053\u308c\u3067\u3042\u308c\u3070\u3001\u30b9\u30e9\u30a4\u30b9\u3092\u524d\u306b\u305a\u3089\u3059\u5fc5\u8981\u304c\u306a\u304f\u306a\u308a\u3001\u3088\u308a\u9ad8\u901f\u306b\u51e6\u7406\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8a00\u8a9e\u306f\u30b7\u30f3\u30d7\u30eb\u3067\u52b9\u7387\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3042\u308a\u3001\u30b9\u30e9\u30a4\u30b9\u306f\u305d\u306e\u4e2d\u3067\u3082\u91cd\u8981\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3059\u3002 PHP\u3067\u306f\u914d\u5217\u95a2\u6570\u304c\u8c4a\u5bcc\u306b\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001\u305d\u306e\u591a\u304f\u306f\u30b9\u30e9\u30a4\u30b9\u306e\u64cd\u4f5c\u3092\u5bb9\u6613\u306b\u3057\u307e\u3059\u3002 \u672c\u8a18\u4e8b\u3067\u306f\u3001Go\u8a00\u8a9e\u3067PHP\u306e\u3044\u304f\u3064\u304b\u306e\u914d\u5217\u95a2\u6570\u3092\u518d\u73fe\u3059\u308b\u2026<\/p>\n","protected":false},"author":3,"featured_media":3059,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-3057","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/3057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/comments?post=3057"}],"version-history":[{"count":0,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/3057\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media\/3059"}],"wp:attachment":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media?parent=3057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/categories?post=3057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/tags?post=3057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}