Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组重新排列_Php_Arrays - Fatal编程技术网

PHP数组重新排列

PHP数组重新排列,php,arrays,Php,Arrays,我有一个数组,看起来像这样: array(3) { ["title"]=> array(3) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(4) "test" } ["file"]=> array(3) { [0]=> string(25) "company_handbook_2011.pdf" [

我有一个数组,看起来像这样:

array(3) {
  ["title"]=>
  array(3) {
    [0]=>
    string(3) "one"
    [1]=>
    string(3) "two"
    [2]=>
    string(4) "test"
  }
  ["file"]=>
  array(3) {
    [0]=>
    string(25) "company_handbook_2011.pdf"
    [1]=>
    string(8) "test.doc"
    [2]=>
    string(8) "test.doc"
  }
  ["status"]=>
  array(3) {
    [0]=>
    string(4) "SHOW"
    [1]=>
    string(4) "HIDE"
    [2]=>
    string(4) "HIDE"
  }
}
array(
    array(
        "one",
        "company_handbook_2011.pdf",
        "SHOW"
    ),
    array(
        "two",
        "test.doc",
        "HIDE"
    ),
    array(
        "test",
        "test.doc",
        "HIDE"
    ),      
)
如何将其重新排列为如下所示:

array(3) {
  ["title"]=>
  array(3) {
    [0]=>
    string(3) "one"
    [1]=>
    string(3) "two"
    [2]=>
    string(4) "test"
  }
  ["file"]=>
  array(3) {
    [0]=>
    string(25) "company_handbook_2011.pdf"
    [1]=>
    string(8) "test.doc"
    [2]=>
    string(8) "test.doc"
  }
  ["status"]=>
  array(3) {
    [0]=>
    string(4) "SHOW"
    [1]=>
    string(4) "HIDE"
    [2]=>
    string(4) "HIDE"
  }
}
array(
    array(
        "one",
        "company_handbook_2011.pdf",
        "SHOW"
    ),
    array(
        "two",
        "test.doc",
        "HIDE"
    ),
    array(
        "test",
        "test.doc",
        "HIDE"
    ),      
)

i、 e获取每个数组的第一个元素并构建一个新数组,然后构建第二个元素,依此类推。非常感谢您的帮助。

您正在搜索的是矩阵的转置。以下是关于StackOverflow的先前答案:

我可以自由地直接从该线程复制解决方案($array\u of\u array是您的原始数组,$transposed\u array是结果):


我想这没有现成的功能。您只需在循环中遍历数组,然后构建它。您不必遍历数组,因为索引似乎在不同的键之间匹配。您可以计算数字
$array['title']
,然后使用一个简单的for循环,从正确的位置获取值来构造一个新数组。我想知道哪种方法更快。你们提供的一些有用的信息非常感谢…感谢你们的时间。数组映射完成了任务。