使用另一个数组作为键创建关联数组
Create an associative array using another array as keys
我目前有 $keys
数组
array(5) {
[0] =>
string(9) "SessionID"
[1] =>
string(5) "Title"
[2] =>
string(11) "Description"
[3] =>
string(5) "Level"
[4] =>
string(4) "Type"
}
,我将其用作另一个名为 $values
的数组的值的键。
我想通过将另一个数组映射到 $keys
来创建关联数组。
另一种说法是我想 array_combine($keys, $values)
而 $keys
只有 5 个元素,而 $values
有超过 3000 个元素。
编辑 1:很抱歉没有放入 $values
示例。它的顺序与 $keys
:
相同
+-----------+-------+-------------+---------+------+
| SESSIONID | TITLE | DESCRIPTION | LEVEL | TYPE |
+-----------+-------+-------------+---------+------+
| | | | | |
| 1 | A | Describe A | Level 1 | Word |
| | | | | |
| 2 | B | Describe B | Level 2 | Word |
+-----------+-------+-------------+---------+------+
或
$values = [
1, 'A', 'Describe A', 'Level 1', 'Word',
2, 'B', 'Describe B', 'Level 2', 'Word'
];
因为我从一个 CSV 文件填充了两个数组。
由于您没有解释 $values
是什么,所以我猜了一点。这里有两种情况。
如果您的值都在同一级别,如下所示,我们可以将它们分块:
$keys = [ "SessionID", "Title", "Description", "Level", "Type", ];
$values = [
1,
"Title A",
"Desc A",
"Monster",
"car",
2,
"Title B",
"Desc B",
"Devil",
"car",
];
将数据切割成长度等于键数的数组。
$chunks = array_chunk($values, count($keys));
然后按照您的建议使用 array_combine
映射它们。
$ass = array_map(function ($chunk) use ($keys) {
return array_combine($keys, $chunk);
}, $chunks);
如果您的数组是数组(或行)的数组,我们可以跳过分块部分并将其直接传递给映射函数:
$values = [
[ 1, "Title A", "Desc A", "Monster", "car" ],
[ 2, "Title B", "Desc B", "Devil", "car" ]
];
$ass = array_map(function ($chunk) use ($keys) {
return array_combine($keys, $chunk);
}, $values);
因为if it's worth doing, it's worth overdoing, here's a slightly more needlessly complex version of the first part of
<?php
$keys = array("SessionID","Title","Description","Level","Type");
$it = new NoRewindIterator(gen_data());
do {
$foo = array_combine(
$keys,
iterator_to_array(new LimitIterator($it, 0, 5))
);
var_export($foo); echo "\r\n";
}
while ( $it->valid() );
function gen_data() {
static $HOW_MANY_WOULD_YOU_LIKE = 100;
$x = array("Session #","Title #","Desc #","Lvl #","Type #");
for($i=0; $i<$HOW_MANY_WOULD_YOU_LIKE*5; $i++) {
yield $x[$i%5].((int)($i/5)+1);
}
}
见http://docs.php.net/language.generators , http://docs.php.net/class.norewinditerator and http://docs.php.net/class.limititerator
数组是如此 2015(该死的 iterator_to_array):-D
我目前有 $keys
数组
array(5) { [0] => string(9) "SessionID" [1] => string(5) "Title" [2] => string(11) "Description" [3] => string(5) "Level" [4] => string(4) "Type" },我将其用作另一个名为
$values
的数组的值的键。
我想通过将另一个数组映射到 $keys
来创建关联数组。
另一种说法是我想 array_combine($keys, $values)
而 $keys
只有 5 个元素,而 $values
有超过 3000 个元素。
编辑 1:很抱歉没有放入 $values
示例。它的顺序与 $keys
:
+-----------+-------+-------------+---------+------+ | SESSIONID | TITLE | DESCRIPTION | LEVEL | TYPE | +-----------+-------+-------------+---------+------+ | | | | | | | 1 | A | Describe A | Level 1 | Word | | | | | | | | 2 | B | Describe B | Level 2 | Word | +-----------+-------+-------------+---------+------+
或
$values = [ 1, 'A', 'Describe A', 'Level 1', 'Word', 2, 'B', 'Describe B', 'Level 2', 'Word' ];
因为我从一个 CSV 文件填充了两个数组。
由于您没有解释 $values
是什么,所以我猜了一点。这里有两种情况。
如果您的值都在同一级别,如下所示,我们可以将它们分块:
$keys = [ "SessionID", "Title", "Description", "Level", "Type", ];
$values = [
1,
"Title A",
"Desc A",
"Monster",
"car",
2,
"Title B",
"Desc B",
"Devil",
"car",
];
将数据切割成长度等于键数的数组。
$chunks = array_chunk($values, count($keys));
然后按照您的建议使用 array_combine
映射它们。
$ass = array_map(function ($chunk) use ($keys) {
return array_combine($keys, $chunk);
}, $chunks);
如果您的数组是数组(或行)的数组,我们可以跳过分块部分并将其直接传递给映射函数:
$values = [
[ 1, "Title A", "Desc A", "Monster", "car" ],
[ 2, "Title B", "Desc B", "Devil", "car" ]
];
$ass = array_map(function ($chunk) use ($keys) {
return array_combine($keys, $chunk);
}, $values);
因为if it's worth doing, it's worth overdoing, here's a slightly more needlessly complex version of the first part of
<?php
$keys = array("SessionID","Title","Description","Level","Type");
$it = new NoRewindIterator(gen_data());
do {
$foo = array_combine(
$keys,
iterator_to_array(new LimitIterator($it, 0, 5))
);
var_export($foo); echo "\r\n";
}
while ( $it->valid() );
function gen_data() {
static $HOW_MANY_WOULD_YOU_LIKE = 100;
$x = array("Session #","Title #","Desc #","Lvl #","Type #");
for($i=0; $i<$HOW_MANY_WOULD_YOU_LIKE*5; $i++) {
yield $x[$i%5].((int)($i/5)+1);
}
}
见http://docs.php.net/language.generators , http://docs.php.net/class.norewinditerator and http://docs.php.net/class.limititerator
数组是如此 2015(该死的 iterator_to_array):-D