douxian1892 2017-05-03 09:58
浏览 78
已采纳

在php中合并两个或多个数组的值

Someone could be so nice to tell me how to do the following with 2 ore more arrays in PHP:

array 1 (a,b,c,d)
array 2 (1,2,3,4)

I would like to merge the two arrays in an unique array with the merged values:

Result: unique array (a-1,b-2,c-3,d-4).

Is there any function that does so? I could not find anything in the forum either on the web.


Thanks for all your answers but I guess that my arrays are a bit more structured because I need the final result for a dropdown field. Now I have these 2 arrays:

$array1[] = array( 'text' => $hospital['value'], 'value' => $hospital['value'] );
$array2[] = array( 'text' => $company['value'], 'value' => $company['value'] );

I want to have a final array that contains: Hospital1 - Company1, Hospital2 - Company2, Hospital3 - Company3, etc..

Thanks

  • 写回答

3条回答 默认 最新

  • dsd119120 2017-05-03 10:06
    关注

    You would have to create a loop to do this manually. it might look something like the following:

    $a = array(a,b,c,d);
    $b = array(1,2,3,4);
    $c = array(); //result set
    if(count($a) == count($b)){ // make sure they are the same length
       for($i = 0; $i < count($a); $i++){
          $c[] = $a[$i]."-".$b[$i];
       }
    }
    print_r($c);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?