我有一个如下的aray,它是一个单维数组。我想使用product键作为值从此数组创建一个二维数组。
如何创建这样的多维数组?
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
)
)
我想获得一个二维数组,将所有产品密钥作为keyas,并将相应的记录作为值显示在下面
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
)
}
答案 0 :(得分: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
正如观察一样,这种结构非常低效,虽然可以通过添加引用等来改进,但您可能需要重新考虑数据结构。