-7

我有

阵列A

Array
(
    [0] => 1440:480
    [1] => thumb
    [2] => 240:auto
    [3] => small-thumb
    [4] => big-thumb
    [5] => detail-img
    [6] => tiny-thumb
)

阵列B

Array
(
    [0] => 1440x480
    [1] => 130xauto
    [2] => 240xauto
    [3] => 100xauto
    [4] => 150xauto
    [5] => 650xauto
    [6] => 45xauto
)

这个怎么做?

阵列C

Array
(
    [1440:480] => 1440x480
    [thumb] => 130xauto
    [240:auto] => 240xauto
    [small-thumb] => 100xauto
    [big-thumb] => 150xauto
    [detail-img] => 650xauto
    [tiny-thumb] => 45xauto
)
4

5 回答 5

8

您可以简单地使用array_combine()

php.net 示例:

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);

输出:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)
于 2013-05-21T12:25:20.000 回答
2
$arr1 = array(0 => '1440:480',
    1 => 'thumb',
    2 => '240:auto',
    3 => 'small-thumb',
    4 => 'big-thumb',
    5 => 'detail-img',
    6 => 'tiny-thumb'
);

$arr2 = array
(
    0 => '1440x480',
    1 => '130xauto',
    2 => '240xauto',
    3 => '100xauto',
    4 => '150xauto',
    5 => '650xauto',
    6 => '45xauto'
);

foreach($arr1 as $key => $val){
    $newArr[$val] = $arr2[$key];
}

print_r($newArr);
于 2013-05-21T12:28:48.153 回答
0

使用array_combine

$c = array_combine($a, $b);
于 2013-05-21T12:26:50.007 回答
0
$a = array(
    "1440:480",
    "thumb",
    "240:auto",
    "small-thumb",
    "big-thumb",
    "detail-img",
    "tiny-thumb"
);

$b = array(
    "1440x480",
    "130xauto",
    "240xauto",
    "100xauto",
    "150xauto",
    "650xauto",
    "45xauto"
);

function merge_arrays($keys, $values) {
   $merged = array();
   for ($i = 0; $i < count($keys); $i++) {
     $merged[$keys[$i]] = $values[$i];
   }
   return $merged;
}

$c = merge_arrays($a, $b);
于 2013-05-21T12:26:57.787 回答
0

不是最复杂的agorythms。猜测两个数组的大小与给定示例中的大小相同,您可以遍历第一个数组并使用它们相同的 ID 组合它们:

$c = array();
foreach($a as $id => $value){
    $c[$value] = $b[$id];
}
于 2013-05-21T12:29:19.827 回答