duanhuizhe6767 2015-11-06 22:15
浏览 20
已采纳

PHP在Array中查找相同的值并创建新数组

I have an array like this:

array 
  0 => 
    array (size=5)
      'id' => string '1' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'info@test.de' (length=16)
  1 => 
    array (size=5)
      'id' => string '3' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'kun@test.de' (length=26)
  2 => 
    array (size=5)
      'id' => string '5' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'ni@test.de' (length=23)
  3 => 
    array (size=5)
      'id' => string '410' (length=3)
      'hostings_id' => string '5' (length=1)
      'email' => string 'spam@email.de' (length=13)
  4 => 
    array (size=5)
      'id' => string '148' (length=3)
      'hostings_id' => string '5' (length=1)
      'email' => string 'e@email.de' (length=23)
  5 => 
    array (size=5)
      'id' => string '165' (length=3)
      'hostings_id' => string '8' (length=1)
      'email' => string 'mobil@newmail.de' (length=15)
  6 => 
    ...

My goal is to put this array into a new array:

array 
  0 => 
    array (size=5)
      'id' => string '1' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'info@test.de' (length=16)
    array (size=5)
      'id' => string '3' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'kun@test.de' (length=26)
    array (size=5)
      'id' => string '5' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'ni@test.de' (length=23)
  1 => 
    array (size=5)
      'id' => string '410' (length=3)
      'hostings_id' => string '5' (length=1)
      'email' => string 'spam@email.de' (length=13)
    array (size=5)
      'id' => string '148' (length=3)
      'hostings_id' => string '5' (length=1)
      'email' => string 'e@email.de' (length=23) 
   2 =>   
    array (size=5)
      'id' => string '165' (length=3)
      'hostings_id' => string '8' (length=1)
      'email' => string 'mobil@newmail.de' (length=15)
  3 => 

At the end I want an JSON like this:

{
    "emails": {
        "0": {
            "id": "1",
            "hostings_id": "4",
            "email": "info@test.de",
        },
        "1": {
            "id": "3",
            "hostings_id": "4",
            "email": "kun@test.de",
        },
        "2": {
            "id": "5",
            "hostings_id": "4",
            "email": "ni@test.de",
        },
    }
}

{
    "emails": {
        "0": {
            "id": "410",
            "hostings_id": "5",
            "email": "spam@email.de",
        },
        "0": {
            "id": "148",
            "hostings_id": "5",
            "email": "e@email.de",
        },
    }
}
...

I want to create the JSON based on the 'hostings_id' Everything what has the same 'hostings_id' value should be together in a separated JSON.

I´ve tried a lot with foreach and array_count_values but did not get the result I want. Thank you very much for you help.

regards

  • 写回答

2条回答 默认 最新

  • doujiaozhan4397 2015-11-06 22:19
    关注

    i think what you need is this:

    array_merge($array1, $array2);
    

    let's say i have the following arrays :

    $array1 = array('id'=>1,'name'=>'james');
    
    $array2 = array('address'=>'usa','phone'=>'912092091');
    
    // merge then and put all together
    
    $array_final = array_merge($array1, $array2);
    
    print_r($array_final);
    

    check the following link for more information:

    http://php.net/manual/it/function.array-merge.php

    and here is your final code including json with same hosting_id but in different json array:

    <?php
    $array1 = array('0'=>array('id'=>5,'hosting_id'=>'4','email'=>'example@sample.com'),'1'=>array('id'=>6,'hosting_id'=>'5','email'=>'example@sample.com'),'2'=>array('id'=>8,'hosting_id'=>'4','email'=>'example@sample.com'),'3'=>array('id'=>10,'hosting_id'=>'5','email'=>'example@sample.com'),'4'=>array('id'=>11,'hosting_id'=>'5','email'=>'example@sample.com'));
    $array2 = array('0'=>array('id'=>78,'hosting_id'=>'4','email'=>'example@sample.com'),'1'=>array('id'=>96,'hosting_id'=>'5','email'=>'example@sample.com'),'2'=>array('id'=>78,'hosting_id'=>'4','email'=>'example@sample.com'),'3'=>array('id'=>110,'hosting_id'=>'5','email'=>'example@sample.com'),'4'=>array('id'=>111,'hosting_id'=>'5','email'=>'example@sample.com'));
    
    // merge then and put all together
    
    $array_final = array_merge($array1, $array2);
    
    $out=array();
    foreach($array_final as $x){
      $out[$x['hosting_id']]['hosting_id']=$x['hosting_id'];
      $out[$x['hosting_id']]['details'][]=array('id'=>$x['id'],'hosting_id'=>$x['hosting_id'],'email'=>$x['email']);
    }
    
    echo json_encode($out);
    

    result:

    {
    4: {
    hosting_id: "4",
    details: [
    {
    id: 5,
    hosting_id: "4"
    },
    {
    id: 8,
    hosting_id: "4"
    },
    {
    id: 78,
    hosting_id: "4"
    },
    {
    id: 78,
    hosting_id: "4"
    }
    ]
    },
    5: {
    hosting_id: "5",
    details: [
    {
    id: 6,
    hosting_id: "5"
    },
    {
    id: 10,
    hosting_id: "5"
    },
    {
    id: 11,
    hosting_id: "5"
    },
    {
    id: 96,
    hosting_id: "5"
    },
    {
    id: 110,
    hosting_id: "5"
    },
    {
    id: 111,
    hosting_id: "5"
    }
    ]
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?