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条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示