![](/img/trans.png)
[英]PHP - efficient way of adding multidimensional array values to a specific key
[英]PHP most efficient way to create an multidimensional array containing the values from list of values
輸入是字符串數組,例如
$strs = [
"some string",
"another string",
// ...
];
結果應為:
$result = [
[
"somestring",
true
],
[
"another string",
true
]
];
該應用程序將為數據提供者創建一個數組,以在單元測試中測試電話號碼。
我可以很容易地在一個循環中執行此操作,但是我想知道是否有一個數組函數。
我的循環解決方案:
$result = [];
foreach($strs as $str) {
$result[] = [$str, true];
}
您可以使用array_map()
代替
$strs = [
"some string",
"another string",
// ...
];
$result = array_map(function($val){
return [$val, true];
}, $strs);
或結合使用array_map()
和array_fill()
$result = array_map(null, $strs, array_fill(0, sizeof($strs), true));
在演示中檢查結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.