如何获得php两个数组值

时间:2015-07-22 08:08:20

标签: php arrays

我有两个阵列:

$arr_1 = array('a','b','c','d');
$arr_2 = array('e','f','g','h');

现在我想以下列格式获取值:

a:e
b:f
c:g
d:h

我该怎么做?谢谢!

3 个答案:

答案 0 :(得分:2)

<?php

$arr_1 = array('a','b','c','d');
$arr_2 = array('e','f','g','h');

$array3 = array_combine($arr_1, $arr_2);
foreach($array3 as $key=>$val){
echo $key.":".$val."<br>";
}

答案 1 :(得分:1)

使用array_combine()

$arr_1 = array(a,b,c,d);
$arr_2 = array(e,f,g,h);
$result = array_combine($arr_1, $arr_2);

答案 2 :(得分:0)

// First, ensure they are the same length    
if(count($arr_1) == count($arr_2)){

  // Then combine them into associative array
  $combined_array = array_combine($arr_1, $arr_2);

  // Check the array is not empty
  if(!empty($combined_array)){

    // Print them out (alternatively put them in an array)
    foreach($combined_array as $key => $value){
      echo $key . ":" . $value . "<br>";
    }
  }
}