我正在尝试将一个值设置为另一个数组。 我有这种类型的两个数组。
Array
(
[0] => test1
[1] => test2
)
Array
(
[0] => 351
[1] => 352
[2] => 353
[3] => 354
[4] => 355
[5] => 356
)
现在我想做一些事情,比如在 test1 上设置第二个数组的前三个值,并将第二个数组中的另外三个值设置到 test2。
test1 = 351,352,353
test2 = 354,355,356
这可能吗?
最佳答案
试试这个:
$var = array(0=> "test1",1=> "test2");
$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);
$res = array_combine($var,array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3)));
echo "<pre>";
print_r($res);
输出:
Array
(
[test1] => 351,352,353
[test2] => 354,355,356
)
编辑:根据评论“我需要这种类型的输出数组([0] => 351,352,353 [1] => 354,355,356)”
$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);
$res = array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3));
echo "<pre>";
print_r($res);
输出:
Array
(
[0] => 351,352,353
[1] => 354,355,356
)
关于php - 使用php将数组值设置为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16728469/