php 2个一维数组合并为1个二维数组,一个数组作为键,另一个作为值:数据是动态从数据库中读取出来的: 第一个数组格式如下:  $arr1 = array(1=>'餐饮美食',2=>'休闲娱乐',3=>'美容化妆',4=>"网上购物",5=>'运动健身',6=>'其他分类'); 第二个数组格式如下:  $str2 = Array(
                    [tuanid] => 1
                    [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
                    [webname] => 17购物团
                    [city] => 北京
                    [title] => 仅29元抢购原价549元的中国联通精品3G手机号!【186号段手机号+内含504元话费+45元卡费】超值优惠,支持网上选号,先到先得!全国无长途、无漫游,接听全免费,赶快体验超极速的3G吧!仅限北京地区购买!商家包邮!
                    [category] => 
                    [content] => 
                    [value] => 549
                    [rebate] => 1
                    [price] => 29
                    [startTime] => 1308758400
                    [endTime] => 1309363200
                    [address] => 
                    [dpshopid] => 0
                    [bought] => 118
                    [goodsurl] => http://17gwt.com/team.php?id=1078
                    [weburl] => http://17gwt.com
                    [count_comment] => 0
                    [count_view] => 0
                    [iscomment] => 0
                )  想得到合并后的数组:  $arr3 = Array(
            ['餐饮美食'] => Array(
                    [tuanid] => 1
                    [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
                    [webname] => 17购物团
                    [city] => 北京
                    [title] => 仅29元抢购原价549元的中国联通精品3G手机号!【186号段手机号+内含504元话费+45元卡费】超值优惠,支持网上选号,先到先得!全国无长途、无漫游,接听全免费,赶快体验超极速的3G吧!仅限北京地区购买!商家包邮!
                    [category] => 
                    [content] => 
                    [value] => 549
                    [rebate] => 1
                    [price] => 29
                    [startTime] => 1308758400
                    [endTime] => 1309363200
                    [address] => 
                    [dpshopid] => 0
                    [bought] => 118
                    [goodsurl] => http://17gwt.com/team.php?id=1078
                    [weburl] => http://17gwt.com
                    [count_comment] => 0
                    [count_view] => 0
                    [iscomment] => 0
                )            ['休闲娱乐'] => Array(
                    [tuanid] => 2
                    [imagesurl] => http://17gwt.com/static/team/2011/0622/13087501332420_index.jpg
                    [webname] => 17购物团
                    [city] => 全国
                    [title] => 原价339元,毕斯曼 2011新款——棒棒糖魔幻系列可爱淑女日韩包包,17购物团仅售129元
                    [category] => 
                    [content] => 
                    [value] => 339
                    [rebate] => 4
                    [price] => 129
                    [startTime] => 1308758400
                    [endTime] => 1308844800
                    [address] => 
                    [dpshopid] => 0
                    [bought] => 318
                    [goodsurl] => http://17gwt.com/team.php?id=1077
                    [weburl] => http://17gwt.com
                    [count_comment] => 0
                    [count_view] => 0
                    [iscomment] => 0
                ) );  先谢谢各位了,

解决方案 »

  1.   

    $str2是个二维数组吧。要不然后面怎么出现两个?
      

  2.   

    /*
    省略了些东西你,你自己填充吧
    */
    $arr1 = array(1=>'餐饮美食',2=>'休闲娱乐',3=>'美容化妆',4=>"网上购物",5=>'运动健身',6=>'其他分类');
    $arr2 = array(
    array('tuanid' => 1, 'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg'),
    array('tuanid' => 1, 'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg'),
    array('tuanid' => 1, 'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg'),
    array('tuanid' => 1, 'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg'),
    array('tuanid' => 1, 'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg'),
    array('tuanid' => 1, 'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg')
    );
    $arr = array_combine($arr1, $arr2);echo '<pre>';print_r($arr);
      

  3.   

    //输出:
    Array
    (
        [餐饮美食] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
            )    [休闲娱乐] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
            )    [美容化妆] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
            )    [网上购物] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
            )    [运动健身] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
            )    [其他分类] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
            ))
      

  4.   


      $arr1 = array(1=>'餐饮美食',2=>'休闲娱乐',3=>'美容化妆',4=>"网上购物",5=>'运动健身',6=>'其他分类');
      $str2 = Array(
       array(
      'tuanid' => 1,
      'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg',
      'webname' => '17购物团',
      'city' => '北京',
      'title' => '仅29元抢购原价549元的中国联通精品3G手机号!【186号段手机号+内含504元话费+45元卡费】超值优惠,支持网上选号,先到先得!全国无长途、无漫游,接听全免费,赶快体验超极速的3G吧!仅限北京地区购买!商家包邮!',
      'category' =>'' ,
      'content' =>'' ,
      'value' => 549,
      'rebate' => 1,
      'price' => 29,
      'startTime' => 1308758400,
      'endTime' => 1309363200,
      'address' =>'' ,
      'dpshopid' => 0,
      'bought' => 118,
      'goodsurl' => 'http://17gwt.com/team.php?id=1078',
      'weburl' => 'http://17gwt.com',
      'count_comment' => 0,
      'count_view' => 0,
      'iscomment' => 0
      ),
      array(
      'tuanid' => 1,
      'imagesurl' => 'http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg',
      'webname' => '17购物团',
      'city' => '全国',
      'title' => '原价339元,毕斯曼 2011新款——棒棒糖魔幻系列可爱淑女日韩包包,17购物团仅售129元',
      'category' =>'' ,
      'content' =>'' ,
      'value' => 549,
      'rebate' => 1,
      'price' => 29,
      'startTime' => 1308758400,
      'endTime' => 1309363200,
      'address' =>'' ,
      'dpshopid' => 0,
      'bought' => 118,
      'goodsurl' => 'http://17gwt.com/team.php?id=1078',
      'weburl' => 'http://17gwt.com',
      'count_comment' => 0,
      'count_view' => 0,
      'iscomment' => 0
      )
      );
      $n=array_values($arr1);
      foreach($str2 as $k=>$v){
           $ar[$n[$k]]=$v;
      } 
      print_r($ar);quit();Array
    (
        [餐饮美食] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
                [webname] => 17购物团
                [city] => 北京
                [title] => 仅29元抢购原价549元的中国联通精品3G手机号!【186号段手机号+内含504元话费+45元卡费】超值优惠,支持网上选号,先到先得!全国无长途、无漫游,接听全免费,赶快体验超极速的3G吧!仅限北京地区购买!商家包邮!
                [category] => 
                [content] => 
                [value] => 549
                [rebate] => 1
                [price] => 29
                [startTime] => 1308758400
                [endTime] => 1309363200
                [address] => 
                [dpshopid] => 0
                [bought] => 118
                [goodsurl] => http://17gwt.com/team.php?id=1078
                [weburl] => http://17gwt.com
                [count_comment] => 0
                [count_view] => 0
                [iscomment] => 0
            )    [休闲娱乐] => Array
            (
                [tuanid] => 1
                [imagesurl] => http://17gwt.com/static/team/2011/0623/13087927794943_index.jpg
                [webname] => 17购物团
                [city] => 全国
                [title] => 原价339元,毕斯曼 2011新款——棒棒糖魔幻系列可爱淑女日韩包包,17购物团仅售129元
                [category] => 
                [content] => 
                [value] => 549
                [rebate] => 1
                [price] => 29
                [startTime] => 1308758400
                [endTime] => 1309363200
                [address] => 
                [dpshopid] => 0
                [bought] => 118
                [goodsurl] => http://17gwt.com/team.php?id=1078
                [weburl] => http://17gwt.com
                [count_comment] => 0
                [count_view] => 0
                [iscomment] => 0
            ))
      

  5.   


    $arr3 = array();
    foreach($arr1 as $value)
    {
        $arr3[$value] = array();
    }
    foreach($arr2 as $k=>$v)
    {
        $arr3['餐饮美食'][$k] = $v;
    }
    你应该还有很多个$arr2格式的数组,不知道你是将这些存在一个大数组里还是单个个的,反正我想你这里$arr2的'tuanid'指的是$arr1里的索引值吧,根据索引id找到对应的类似$arr2数组,然后按上面代码差不多意思改改就好了
      

  6.   

    array_combine(array1,array2)
    函数通过合并两个数组来创建一个新数组,其中的一个数组是键名,另一个数组的值为键值。如果其中一个数组为空,或者两个数组的元素个数不同,则该函数返回 false。其中array1 是规定的键名,array2 则为值
      

  7.   

    上面几位都测试了下,还没有成功,还有 因为两个数组键不相同,这个array_combine(array1,array2)用不了,
      

  8.   

    $arr1 数组数据结构:
    $arr2 数组数据结构:
    想实现如下:$arr1和$arr2合并为一个新数组,新数组以$arr1的值为键,以$arr2为值,就是把$arr2中属于$arr1每个分类的数据中前几条列出来,如下:array(  '餐饮美食'=>array(              '0'=>array(                   'tuanid'=>1,                   'webname'=>'百度'                   );
                  '1'=>array(                   'tuanid'=>90,                   'webname'=>'谷歌'                   );
                  '2'=>array(                   'tuanid'=>123,                   'webname'=>'腾讯'                   );              );  '休闲娱乐'=>array(              '0'=>array(                   'tuanid'=>20,                   'webname'=>'百度'                   );
                  '1'=>array(                   'tuanid'=>61,                   'webname'=>'谷歌'                   );
                  '2'=>array(                   'tuanid'=>92,                   'webname'=>'腾讯'                   );              ););
      

  9.   


    不对应就改为对应不就得了
    array_combine(array_values(array1),array2)
      

  10.   

    像三楼那样写不就行了吗   $arr2 = array(array(),array(),array(),array())