如何从数组中提取和组合数组

时间:2014-11-13 05:49:44

标签: php arrays

我的print_r结果如下:

Array
(
    [0] => A, B, C, D
    [1] => 15,20,24,19
)

如何让它们像这样:

Array
(
    [A] => 15
    [B] => 20
    [C] => 24
    [D] => 19
)

非常感谢您的帮助:)

4 个答案:

答案 0 :(得分:1)

试试这个:

$a = array('A', 'B', 'C', 'D');
$b = array(15, 20, 24, 19);
$c = array();

foreach ($a as $index => $value) {
    if (isset($b[$index])) {
        $c[$value] = $b[$index];
    }
}

var_dump($c);

答案 1 :(得分:0)

array_combine是方式

<?php

$myArray = array(
    array('A', 'B', 'C', 'D'),
    array(15, 20, 24, 19)
);

$combinedArray = array_combine($myArray[0], $myArray[1]);

答案 2 :(得分:0)

尝试用逗号explode()数组索引,并使用array_combine()

将数组与键和值结合起来
$a = explode(',',$arr[0]);
$b = explode(',',$arr[1]);
$new = array_combine($a,$b);
print_r($new); //Array ( [A] => 15 [ B] => 20 [ C] => 24 [ D] => 19 ) 

答案 3 :(得分:0)

您需要explode()array_combine()。假设您的初始数组为$arr

$new_arr = array_combine(explode(', ', $arr[0]), explode(',', $arr[1]));

See demo