使用PHP创建置矩阵 [英] create transpose matrix using php
问题描述
例如,如果一个矩阵是:
1 2
3 4
5 6
然后转以上矩阵将是:
1 3 5
2 4 6
这是我目前的code:
< PHP //矩阵转置
$反=阵列(
阵列(1,2),
阵列(3,4),
阵列(5,6)
); 的foreach($反为$关键=> $ VAL){
的foreach($反[$关键]为$ K => $ V){
回声$ V;
}
}?>
有一个古怪的PHP的方式转一个二维数组:
$反=阵列(
阵列(1,2),
阵列(3,4),
阵列(5,6)
);array_unshift($反,NULL);
$反= call_user_func_array('array_map',$反式);
后续代码var_dump($反式);
修改说明
从PHP文档报价为 array_map()函数:
这是有趣的使用这个功能是构造一个数组的数组,可以通过使用NULL作为回调函数的名称
容易进行
块引用>(见例#4从文档页面的例子这是什么一样)
的
array_unshift($反,空)
,我们进行第一个提供了NULL回调,而我们使用call_user_func_array()
,因为我们并不一定知道有多少价值有我们的$反
阵列。我们现在使用的call_user_func_array()
是相当于做:$反= array_map(NULL,$转[0],$反式[1],$反式[2]);
您的示例阵列,因为你的2-D阵列的顶层有三个要素(键0,1和2)。
实际上,这NULL回调以通过所有的阵列并行取每个值从他们又建立一个新的数组循环:
$ maxArraySize = MAX(计数($数组[0],$阵列[1],$阵列[2]);
// $ maxArraySize将有2的值,你的情况,
//因为你个子阵列都是平等的大小
$ newArray = [];
为($ I = 0; $ I< $ maxArraySize ++ $ I){
$ tmpArray = [];
$ tmpArray [] = $阵列[0] [$ i];
$ tmpArray [] = $阵列[1] [$ i];
$ tmpArray [] = $阵列[2] [$ i];
$ newArray [] = $ tmpArray [];
}那里面有一对夫妇的额外检查
- 它并不关心,如果你的数组是关联或在任何方面列举,因为它访问
$ I
个元素,而不是指数- 如果子阵列都不尽相同的长度,然后将它有效地垫较短的子阵列,空值匹配最长
的长度- 不要紧,你有多少数组传递中,它会与他们都在并行
工作For example if a matrix is:
1 2 3 4 5 6
Then transpose of above matrix will be:
1 3 5 2 4 6
This is my current code:
<?php // transpose matrix $trans = array( array(1, 2), array(3, 4), array(5, 6) ); foreach ($trans as $key => $val){ foreach ($trans[$key] as $k => $v){ echo $v; } } ?>
解决方案There's a quirky PHP way to transpose a 2d array:
$trans = array( array(1, 2), array(3, 4), array(5, 6) ); array_unshift($trans, null); $trans = call_user_func_array('array_map', $trans); var_dump($trans);
EDIT Explanation
Quoting from the PHP docs for the array_map() function:
An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function
(See Example #4 from that docs page for an example of what this does)
The
array_unshift($trans, null)
that we perform first is providing that NULL callback, and we usecall_user_func_array()
because we don't necessarily know how many values there are in our$trans
array. What we're doing using thatcall_user_func_array()
is the equivalent of:$trans = array_map(NULL, $trans[0], $trans[1], $trans[2]);
for your example array, because the top-level of your 2-d array has three elements (keys 0, 1 and 2).
Effectively, this NULL callback loops through all the arrays in parallel taking each value from them in turn to build a new array:
$maxArraySize = max(count($array[0], $array[1], $array[2]); // $maxArraySize will have a value of 2 in your case, // because your sub-arrays are all equal size $newArray = []; for($i = 0; $i < $maxArraySize; ++$i) { $tmpArray = []; $tmpArray[] = $array[0][$i]; $tmpArray[] = $array[1][$i]; $tmpArray[] = $array[2][$i]; $newArray[] = $tmpArray[]; }
There's a couple of extra checks in there
- it doesn't care if your arrays are associative or enumerated in either dimension, because it accesses the
$i
th element, not the index- If the sub-arrays aren't all the same length, then it effectively pads the shorter sub-arrays with null values to match the length of the longest
- It doesn't matter how many arrays you pass in, it will work with them all in parallel
这篇关于使用PHP创建置矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!