2012-02-14 145 views
0

我有一個aray,如下所示,它是一個單維數組。我想使用產品密鑰作爲值從該數組創建一個2維數組。基於密鑰從現有陣列創建新陣列

如何創建這樣的多維數組?

Array 
(
    [0] => Array 
     (
      [0] => God 
      [listitemname] => God 
      [1] => 20a3208eca59 
      [upload_key] => 20a3208eca59 
      [2] => d296772dd9045b1d99e0ff5a 
      [file_key] => d296772dd9045b1d99e0ff5a 
      [3] => a4e4eb6d7621e585d66b424ece08 
      [folderkey] => a4e4eb6d7621e585d66b424ece08 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 1 
      [file_type] => 1 
     ) 

    [1] => Array 
     (
      [0] => God 
      [listitemname] => God 
      [1] => 877ede85abd9 
      [upload_key] => 877ede85abd9 
      [2] => 5a769542b3f75c78a6fd9bda 
      [file_key] => 5a769542b3f75c78a6fd9bda 
      [3] => dfcc7d5523de534dd1d148a67f6f 
      [folderkey] => dfcc7d5523de534dd1d148a67f6f 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 2 
      [file_type] => 2 
     ) 

    [2] => Array 
     (
      [0] => God 
      [listitemname] => God 
      [1] => 32d8dbdb7d31 
      [upload_key] => 32d8dbdb7d31 
      [2] => 413057623b1d11617e4eba71 
      [file_key] => 413057623b1d11617e4eba71 
      [3] => 3cd3bab180cc1ce6350ec6b29c46 
      [folderkey] => 3cd3bab180cc1ce6350ec6b29c46 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 4 
      [file_type] => 4 
     ) 

    [3] => Array 
     (
      [0] => God 
      [listitemname] => God 
      [1] => 93854a71f69e 
      [upload_key] => 93854a71f69e 
      [2] => ddd697e3ec13dd49a987f17b 
      [file_key] => ddd697e3ec13dd49a987f17b 
      [3] => e41ff80e0a18b6cb4cf874730b52 
      [folderkey] => e41ff80e0a18b6cb4cf874730b52 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 5 
      [file_type] => 5 
     ) 

    [4] => Array 
     (
      [0] => Games 
      [listitemname] => Games 
      [1] => 17eec2f803be 
      [upload_key] => 17eec2f803be 
      [2] => 54f74c98ecb267ee5da74da1 
      [file_key] => 54f74c98ecb267ee5da74da1 
      [3] => dc035e3347f429f4b12d1af00d7a 
      [folderkey] => dc035e3347f429f4b12d1af00d7a 
      [4] => 4c8a0251e489 
      [productkey] => 4c8a0251e489 
      [5] => 1 
      [file_type] => 1 
     ) 

    [5] => Array 
     (
      [0] => Games 
      [listitemname] => Games 
      [1] => df9c2041eff0 
      [upload_key] => df9c2041eff0 
      [2] => d65e22242ca45cbf935af4b9 
      [file_key] => d65e22242ca45cbf935af4b9 
      [3] => 45f1ace431926347aa1fcf1ae2c6 
      [folderkey] => 45f1ace431926347aa1fcf1ae2c6 
      [4] => 4c8a0251e489 
      [productkey] => 4c8a0251e489 
      [5] => 2 
      [file_type] => 2 
     ) 

    [6] => Array 
     (
      [0] => Games 
      [listitemname] => Games 
      [1] => 934380f97ae1 
      [upload_key] => 934380f97ae1 
      [2] => 684505352bb62e8c61ea8c8c 
      [file_key] => 684505352bb62e8c61ea8c8c 
      [3] => 2b86c8f004f97bad9600f0e23eed 
      [folderkey] => 2b86c8f004f97bad9600f0e23eed 
      [4] => 4c8a0251e489 
      [productkey] => 4c8a0251e489 
      [5] => 4 
      [file_type] => 4 
     ) 

    [7] => Array 
     (
      [0] => Games 
      [listitemname] => Games 
      [1] => c85be159256e 
      [upload_key] => c85be159256e 
      [2] => eee3f59502b57e07fd83a9b1 
      [file_key] => eee3f59502b57e07fd83a9b1 
      [3] => 9ec2158c662b2c0068308bc461fa 
      [folderkey] => 9ec2158c662b2c0068308bc461fa 
      [4] => 4c8a0251e489 
      [productkey] => 4c8a0251e489 
      [5] => 5 
      [file_type] => 5 
     ) 

) 

我想獲得一個2維數組,其所有的產品密鑰爲keyas和coresponding記錄的值顯示如下

Array 
(
    [d9ef5a3cacbc] => Array{ 
     [0](
      [0] => God 
      [listitemname] => God 
      [1] => 20a3208eca59 
      [upload_key] => 20a3208eca59 
      [2] => d296772dd9045b1d99e0ff5a 
      [file_key] => d296772dd9045b1d99e0ff5a 
      [3] => a4e4eb6d7621e585d66b424ece08 
      [folderkey] => a4e4eb6d7621e585d66b424ece08 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 1 
      [file_type] => 1 
     ) 
    [1] (
      [0] => God 
      [listitemname] => God 
      [1] => 877ede85abd9 
      [upload_key] => 877ede85abd9 
      [2] => 5a769542b3f75c78a6fd9bda 
      [file_key] => 5a769542b3f75c78a6fd9bda 
      [3] => dfcc7d5523de534dd1d148a67f6f 
      [folderkey] => dfcc7d5523de534dd1d148a67f6f 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 2 
      [file_type] => 2 
     ) 
    [2]  (
      [0] => God 
      [listitemname] => God 
      [1] => 32d8dbdb7d31 
      [upload_key] => 32d8dbdb7d31 
      [2] => 413057623b1d11617e4eba71 
      [file_key] => 413057623b1d11617e4eba71 
      [3] => 3cd3bab180cc1ce6350ec6b29c46 
      [folderkey] => 3cd3bab180cc1ce6350ec6b29c46 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 4 
      [file_type] => 4 
     ) 
    [3] (
      [0] => God 
      [listitemname] => God 
      [1] => 93854a71f69e 
      [upload_key] => 93854a71f69e 
      [2] => ddd697e3ec13dd49a987f17b 
      [file_key] => ddd697e3ec13dd49a987f17b 
      [3] => e41ff80e0a18b6cb4cf874730b52 
      [folderkey] => e41ff80e0a18b6cb4cf874730b52 
      [4] => d9ef5a3cacbc 
      [productkey] => d9ef5a3cacbc 
      [5] => 5 
      [file_type] => 5 


    } 

    [4c8a0251e489] => Array{ 
     [0](
      [0] => Games 
      [listitemname] => Games 
      [1] => 17eec2f803be 
      [upload_key] => 17eec2f803be 
      [2] => 54f74c98ecb267ee5da74da1 
      [file_key] => 54f74c98ecb267ee5da74da1 
      [3] => dc035e3347f429f4b12d1af00d7a 
      [folderkey] => dc035e3347f429f4b12d1af00d7a 
      [4] => 4c8a0251e489 
      [productkey] => 4c8a0251e489 
      [5] => 1 
      [file_type] => 1 
     ) 
    [1] (
[0] => Games 
      [listitemname] => Games 
      [1] => 934380f97ae1 
      [upload_key] => 934380f97ae1 
      [2] => 684505352bb62e8c61ea8c8c 
      [file_key] => 684505352bb62e8c61ea8c8c 
      [3] => 2b86c8f004f97bad9600f0e23eed 
      [folderkey] => 2b86c8f004f97bad9600f0e23eed 
      [4] => 4c8a0251e489 
      [productkey] => 4c8a0251e489 
      [5] => 4 
      [file_type] => 4 
     ) 
    [2]  (
      [0] => Games 
      [listitemname] => Games 
      [1] => c85be159256e 
      [upload_key] => c85be159256e 
      [2] => eee3f59502b57e07fd83a9b1 
      [file_key] => eee3f59502b57e07fd83a9b1 
      [3] => 9ec2158c662b2c0068308bc461fa 
      [folderkey] => 9ec2158c662b2c0068308bc461fa 
      [4] => 4c8a0251e489 
      [productkey] => 4c8a0251e489 
      [5] => 5 
      [file_type] => 5 
     ) 

} 
+3

這實際上並不是一個單一的維數組 – Julien 2012-02-14 21:11:29

+2

,它已經是爲2維數組 – Gordon 2012-02-14 21:13:11

+0

是啊,我覺得你的例子是有點靠不住...... – 2012-02-14 21:13:57

回答

2

不知道你已經嘗試過什麼,但是這看起來喜歡它做你需要它的東西。現在

$res = array(); 

// assuming you want an array by product key 
foreach($outer as $inner) 
{ 
    $key = $inner['productKey']; 
    if(!isset($res[$key])) 
     $res[$key] = array(); 
    $res[$key][] = $inner; 
} 
// $res now holds what you're looking for 

,它看起來像你正在尋找一些稍微複雜些:

function not_numeric($a){return !is_numeric($a);} 

// you only care about the non-numeric keys of the arrays. 
$keys = array_map('not_numeric', array_keys($outer[0])); 
$res = array(); 
foreach($keys as $key) 
{ 
    // same idea as above, only nested this time. 
    if(!isset($res[$key])) 
     $res[$key] = array(); 
    // this is exactly the same as the first example, only with an additional 
    // lookup. You can make this better by judicious use of references, but 
    // that is an exercise up to the reader. 
    foreach($outer as $inner) 
    { 
     $inKey = $inner[$key]; 
     if(!isset($res[$key][$inKey])) 
      $res[$key][$inKey] = array(); 
     $res[$key][$inKey][] = $inner; 
    } 
} 

// $res now holds what you're looking for 

只是作爲一個觀察,這種結構是非常低效的,雖然它可能會添加引用和改善比如,你可能想重新考慮你的數據結構。