在 PHP 7.4 中重新索引多维数组的重构实现
1、查看现有实现代码如下:
1 2 3 4 5 | $data = $res [ 'body' ][ 'data' ]; print_r( $data ); $data = array_combine (array_column( $data , 'name' ), $data ); print_r( $data ); exit ; |
2、查看多维数组索引前后的打印结果。使用数组中 name 列的值作为新的键。如图1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | Array ( [0] => Array ( [id] => 8 [user_id] => 6659 [name] => athena [status] => published [title_en] => Athena [title_cn] => 雅典娜 [description_en] => Athena is a template with massive decoration modules. Its social, festive, interactive elements can make a strong impact. Meanwhile, it is committed to creating a unique shopping experience on mobile phones. Suitable for all middle and high-end brands. [description_cn] => Athena是一款装修模块极其丰富的模板,以社交、节日气氛、页面交互等元素带来极强的冲击,同时致力于打造移动端独特的购物体验,适合中高端品牌站。 ) [1] => Array ( [id] => 7 [user_id] => 6659 [name] => vogue [status] => published [title_en] => Vogue [title_cn] => Vogue [description_en] => Vogue adds a video module that can fully display product details. It is a set of templates designed specifically for brand and product stories. [description_cn] => Vogue 模板增加了视频模块,可以全面的展示产品细节,是专为品牌和产品故事而设计的一套模板。 ) [2] => Array ( [id] => 6 [user_id] => [name] => venue [status] => published [title_en] => Volume [title_cn] => Volume [description_en] => Volume uses a clean design style and adds a picture and text module, which is very suitable for high-end brand products. [description_cn] => Volume模板使用干净的设计风格,增加了图片文字模块,非常适合高端品牌产品。 ) [3] => Array ( [id] => 5 [user_id] => 6659 [name] => showtime [status] => published [title_en] => showtime [title_cn] => showtime [description_en] => showtime [description_cn] => showtime ) [4] => Array ( [id] => 4 [user_id] => 6659 [name] => basic [status] => published [title_en] => Basic [title_cn] => Basic [description_en] => Basic is our iconic template, very versatile and concise, suitable for most styles of shops. [description_cn] => Basic 是我们标志性的模板,非常通用以及简洁,适合大部分风格的店铺。 ) [5] => Array ( [id] => 3 [user_id] => 6659 [name] => showcase [status] => published [title_en] => showcase [title_cn] => showcase [description_en] => showcase [description_cn] => showcase ) [6] => Array ( [id] => 2 [user_id] => 6659 [name] => volume [status] => published [title_en] => volume [title_cn] => volume [description_en] => volume [description_cn] => volume ) [7] => Array ( [id] => 1 [user_id] => 6659 [name] => test [status] => published [title_en] => test [title_cn] => test [description_en] => test [description_cn] => test ) ) Array ( [athena] => Array ( [id] => 8 [user_id] => 6659 [name] => athena [status] => published [title_en] => Athena [title_cn] => 雅典娜 [description_en] => Athena is a template with massive decoration modules. Its social, festive, interactive elements can make a strong impact. Meanwhile, it is committed to creating a unique shopping experience on mobile phones. Suitable for all middle and high-end brands. [description_cn] => Athena是一款装修模块极其丰富的模板,以社交、节日气氛、页面交互等元素带来极强的冲击,同时致力于打造移动端独特的购物体验,适合中高端品牌站。 ) [vogue] => Array ( [id] => 7 [user_id] => 6659 [name] => vogue [status] => published [title_en] => Vogue [title_cn] => Vogue [description_en] => Vogue adds a video module that can fully display product details. It is a set of templates designed specifically for brand and product stories. [description_cn] => Vogue 模板增加了视频模块,可以全面的展示产品细节,是专为品牌和产品故事而设计的一套模板。 ) [venue] => Array ( [id] => 6 [user_id] => [name] => venue [status] => published [title_en] => Volume [title_cn] => Volume [description_en] => Volume uses a clean design style and adds a picture and text module, which is very suitable for high-end brand products. [description_cn] => Volume模板使用干净的设计风格,增加了图片文字模块,非常适合高端品牌产品。 ) [showtime] => Array ( [id] => 5 [user_id] => 6659 [name] => showtime [status] => published [title_en] => showtime [title_cn] => showtime [description_en] => showtime [description_cn] => showtime ) [basic] => Array ( [id] => 4 [user_id] => 6659 [name] => basic [status] => published [title_en] => Basic [title_cn] => Basic [description_en] => Basic is our iconic template, very versatile and concise, suitable for most styles of shops. [description_cn] => Basic 是我们标志性的模板,非常通用以及简洁,适合大部分风格的店铺。 ) [showcase] => Array ( [id] => 3 [user_id] => 6659 [name] => showcase [status] => published [title_en] => showcase [title_cn] => showcase [description_en] => showcase [description_cn] => showcase ) [volume] => Array ( [id] => 2 [user_id] => 6659 [name] => volume [status] => published [title_en] => volume [title_cn] => volume [description_en] => volume [description_cn] => volume ) [test] => Array ( [id] => 1 [user_id] => 6659 [name] => test [status] => published [title_en] => test [title_cn] => test [description_en] => test [description_cn] => test ) ) |
3、但是,仅使用函数 array_column 就足以实现了。当 column_key 为 null 时,此时将返回整个数组(配合 index_key 参数来重新索引数组时非常好用)。没有必要再使用额外的函数 array_combine。
1 2 3 4 5 6 | $data = $res [ 'body' ][ 'data' ]; // print_r($data); // $data = array_combine(array_column($data, 'name'), $data); $data = array_column( $data , null, 'name' ); print_r( $data ); exit ; |
4、查看多维数组索引前后的打印结果。符合预期。如图2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | Array ( [athena] => Array ( [id] => 8 [user_id] => 6659 [name] => athena [status] => published [title_en] => Athena [title_cn] => 雅典娜 [description_en] => Athena is a template with massive decoration modules. Its social, festive, interactive elements can make a strong impact. Meanwhile, it is committed to creating a unique shopping experience on mobile phones. Suitable for all middle and high-end brands. [description_cn] => Athena是一款装修模块极其丰富的模板,以社交、节日气氛、页面交互等元素带来极强的冲击,同时致力于打造移动端独特的购物体验,适合中高端品牌站。 ) [vogue] => Array ( [id] => 7 [user_id] => 6659 [name] => vogue [status] => published [title_en] => Vogue [title_cn] => Vogue [description_en] => Vogue adds a video module that can fully display product details. It is a set of templates designed specifically for brand and product stories. [description_cn] => Vogue 模板增加了视频模块,可以全面的展示产品细节,是专为品牌和产品故事而设计的一套模板。 ) [venue] => Array ( [id] => 6 [user_id] => [name] => venue [status] => published [title_en] => Volume [title_cn] => Volume [description_en] => Volume uses a clean design style and adds a picture and text module, which is very suitable for high-end brand products. [description_cn] => Volume模板使用干净的设计风格,增加了图片文字模块,非常适合高端品牌产品。 ) [showtime] => Array ( [id] => 5 [user_id] => 6659 [name] => showtime [status] => published [title_en] => showtime [title_cn] => showtime [description_en] => showtime [description_cn] => showtime ) [basic] => Array ( [id] => 4 [user_id] => 6659 [name] => basic [status] => published [title_en] => Basic [title_cn] => Basic [description_en] => Basic is our iconic template, very versatile and concise, suitable for most styles of shops. [description_cn] => Basic 是我们标志性的模板,非常通用以及简洁,适合大部分风格的店铺。 ) [showcase] => Array ( [id] => 3 [user_id] => 6659 [name] => showcase [status] => published [title_en] => showcase [title_cn] => showcase [description_en] => showcase [description_cn] => showcase ) [volume] => Array ( [id] => 2 [user_id] => 6659 [name] => volume [status] => published [title_en] => volume [title_cn] => volume [description_en] => volume [description_cn] => volume ) [test] => Array ( [id] => 1 [user_id] => 6659 [name] => test [status] => published [title_en] => test [title_cn] => test [description_en] => test [description_cn] => test ) ) |
近期评论