简体   繁体   English

PHP 中的数组和 integer 映射

[英]Array and integer mapping in PHP

Inputs输入

hello every I have array-like this 你好,我有这样的数组
 $A=[1,2,3]; $B=1;

but I want output like this但我想要这样的 output

Output Output

 [{1,1},{1,2},{1,3}]

For example like this:例如像这样:

$A = [1, 2, 3];
$B = 1;
$result = [];

foreach ($A as $key => $value) {
    $result[] = [$B, $value];
}

print_r(json_encode($result)); // [[1,1],[1,2],[1,3]]

Or like this:或者像这样:

$A = [1, 2, 3];
$B = 1;

array_walk($A, function(&$item) use ($B) { $item = [$B, $item]; });

print_r(json_encode($A)); // [[1,1],[1,2],[1,3]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM